[Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then3steps to the last index.)
题意:问最小的跳跃次数
思路:这题是jump game的扩展。这题有两种解法:
第一种是动态规划,定义数组dp,dp[i]表示到达i处的最小跳跃次数。关键在确定dp[i]的值,我们可以遍历i之前的元素值,找到第一次能跳到或跳过i的下标,然后在其基础上加1即可。代码如下:
class Solution {
public:
int jump(int A[], int n)
{
vector<int> dp(n,);
if(n==) return ;
for(int i=;i<n;i++)
{
for(int j=;j<i;++j)
{
if(A[j]+j>=i)
{
dp[i]=dp[j]+;
break;
}
}
}
return dp[n-];
}
};
这种解法大的集合过不了。
第二种是贪心算法。其实个人觉得两种思路差不多,只是这种方法去掉了一些不必要的计算。遍历数组,找到当前能到达的最远距离curr,则,在这个距离中,不断的更新能到达的最远距离maxlen,遍历数组,若出了curr的范围,则计数器加1,并重新更新cur,至于i是如何出cur的范围不考虑。如:{3,3,1,1,4},

第一个3能达到的最远距离是curr,遍历数组,记下3到curr之间最新的最大距离maxlen,当i大于curr时,我们可以一步到达maxlen所在的位置,也就是说,我们不关心,从3到curr是如何到达的。参考了实验室小纸贴校外版的博客。代码如下:
class Solution {
public:
int jump(int A[], int n)
{
int curr=,maxLen=,count=;
for(int i=;i<n;++i)
{
if(i>curr)
{
curr=maxLen;
count++;
}
maxLen=max(maxLen,i+A[i]);
}
return count;
}
};
[Leetcode] jump game ii 跳跃游戏的更多相关文章
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)
https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- ogg的安装配置 配置双向同步(含DDL)
第一部分 先配置单向同步(含DDL) 一 源端安装GoldenGate 创建用户 创建目录 mkdir -p /opt/ogg chmod -R 777 /opt/ogg chown -R oracl ...
- hdu2066一个人的旅行(floyd优化)
一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- CSS随笔3
1. CSS部分简洁使用 * background-radious:使得边框角“圆化”. * background:pink url(“图片路径”) no-repeat: * border 可以有 ...
- jmeter基础之录制篇
一.前言 jmeter如今被越来越多人喜爱的一款测试工具,相比于loadrunner它体积特轻便.jmeter不仅用来做单接口测试,压测还能做性能,主要是一款开源的,可以写一个你需要的插件功能再添加里 ...
- TensorFlow入门之MNIST最佳实践-深度学习
在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...
- OpenCV学习5-----使用Mat合并多张图像
最近做实验需要对比实验结果,需要将几张图片拼在一起,直观对比. 尝试用OpenCV解决. 核心思想其实是 声明一个足够大的,正好容纳下那几张图片的mat,然后将拼图依次copy到大图片相应的位置. ...
- 【转】Linux内核结构详解
Linux内核主要由五个子系统组成:进程调度,内存管理,虚拟文件系统,网络接口,进程间通信. 1.进程调度 (SCHED):控制进程对CPU的访问.当需要选择下一个进程运行时,由调度程序选择最值得运行 ...
- su和sudo的使用
用于用户身份切换 一.su 命令形式 代表内容 su 切换为root,以non-login shell的方式 su - 切换为root,以login shell的方式 su -l 账号 切换为“账号” ...
- 软件工程课堂作业(二)续——升级完整版随机产生四则运算题目(C++)
一.设计思想: 1.根据题目新设要求,我将它们分为两类:一类是用户输入数目,根据这个数目改变一系列后续问题:另一类是用户输入0或1,分情况解决问题. 2.针对这两类要求,具体设计思路已在上篇博文中写出 ...
- Android UI 设计之 TextView EditText 组件属性方法最详细解析
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...