【To Read】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 is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
思路:
第一种思路是利用迭代的思路来计算最小跳数,但是时间复杂度比较大;第二种思路是反过来想,要达到最后一条,倒数第二条至少应该到哪个位置,以此类推直到我们倒推到第一位时便可知最小跳数;第三种思路是用动态规划DP的观点来实现。DP[i]代表到达i的最小跳数,显然DP是一个递增的数组。每次循环只需要尽量找到最小的DP[k],使其满足k+A[k]>=n。
代码:
思路一:迭代(时间复杂度不满足要求)
- class Solution {
- public:
- int jump(int A[], int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- int* v = new int[1];
- v[0] = INT_MAX;
- jumpRepeat(A, 0, n-1, 0, v);
- if(v[0] == INT_MAX)
- {
- return 0;
- }
- return v[0];
- }
- void jumpRepeat(int A[], int i, int m, int n,int* v)
- {
- if(i >= m)
- {
- if(v[0] > n)
- {
- v[0] = n;
- }
- return;
- }
- if(A[i] == 0)
- {
- return;
- }
- else
- {
- for(int j = 1; j <= A[i]; j++)
- {
- jumpRepeat(A, i + j, m, n+1, v);
- }
- }
- }
- };
- class Solution {
- public:
- int jump(int A[], int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- int pre = 0;
- int cur = n - 1;
- int count = 0;
- while(true)
- {
- if(pre == cur)
- {
- return 0;
- }
- count++;
- pre = cur;
- for(int i = n - 2; i >= 0; i--)
- {
- if(i + A[i] >= pre)
- {
- if(cur > i)
- {
- cur = i;
- }
- }
- }
- if(cur == 0)
- {
- return count;
- }
- };
- }
- };
思路三:动态规划
- class Solution {
- public:
- int* dp;
- int jump(int A[], int n) {
- if(n==0)
- {
- return INT_MAX;
- }
- dp = new int[n];
- dp[0] = 0;
- for(int i=1;i<n;i++)
- {
- dp[i] = INT_MAX;
- }
- for(int i=1;i<n;i++)
- {
- for(int j=0;j<i;j++)
- {
- if(j+A[j]>=i)
- {
- int tmp = dp[j]+1;
- if(tmp < dp[i])
- {
- dp[i] = tmp;
- break;
- }
- }
- }
- }
- return dp[n-1];
- }
- };
【To Read】LeetCode | Jump Game II(转载)的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode–jump game II
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- hashMap 源码解读理解实现原理和hash冲突
hashMap 怎么说呢. 我的理解是 外表是一个set 数组,无序不重复 . 每个set元素是一个bean ,存着一对key value 看看代码吧 package test; import jav ...
- JS的闭包问题
1.什么是“闭包” 是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 2.闭包的应用场景 (1)保护变量的安全实现JS私有属性和私有方法 (2)在 ...
- nginx 下开启pathinfo模式
前几天自己新弄了个服务器,nginx的环境, 看到thinkcmf的框架,下载下来准备研究下,安装完成后,发现 url 是 普通模式,然后我就按照那个手册去后台开启了pathinfo模式,这一改完蛋了 ...
- Docx 生成word文档
1.生成word代码 /// <summary> /// 生成word文档 /// </summary> /// <param name="tempPath&q ...
- hdu 5823 color II——子集dp(独立集)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5823 独立集染一种颜色.在这个基础上枚举子集来dp. 状压一样地存边真是美妙. 2^32是1ll<& ...
- Wamp Apache 启动失败检测方法
一般情况下,看错误日志就可以解决.如果遇到错误日志看不到的情况,不放试试下面的方法 //无错误日志解决办法cmd命令行切换到C:\wamp\bin\apache\apache2.4.9\bin目录 输 ...
- python安装和环境变量配置
python环境安装 一.打开官网:http://www.python.org 点击Downloads下载,如下图 python官网 二.根据电脑型号选择下载的版本 下载对应版本号的executabl ...
- ztree树节点重叠问题
使用zTree时,由于同时使用了bootstrap插件,导致样式起了冲突,生成的树都挤在一起了, 最后的解决办法是设置zTreeStyle.css文件的.ztree li ul{}属性,在里面加入he ...
- 看完就会用的GIT操作图解分析
无论你是前端还是后台,无论是运维还是移动端研发,GIT是逃避不了的东西,当然你说你要用SVN,那不在这次的讨论范围之内.不多说,请看下文GIT图解分析,10分钟学会git操作,当然下面的教程是为实战为 ...
- XML解析器之JAXP与DOM4J
XML是一种数据格式,那么需要对XML文件进行操作就需要用到XML解析器---------针对dom方式和sax方式提供了不同的解析技术-----需要不同的XML解析器 dom方式:会把文档中所有元素 ...