【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 ...
随机推荐
- python 为 class 添加新的属性和方法
通过继承: >>> class Point(namedtuple('Point', ['x', 'y'])): ... __slots__ = () ... @property .. ...
- 2.快速创建springboot项目 连pom文件里面的配置都不用配了
无论是创建项目 还是module 模块 选择这个 .然后在后面的选择中选择自己要的功能 就可以把相关的依赖都加进去 省去了依赖 其后的写法跟第一篇一样 在这个项目下面有一个配置文件 ====>a ...
- webServices学习四(---WebService监听工具)
之前我们使用过HttpWatch获取的HTTP的调用过程,并获得了HTTP的请求头及其他请求的详细信息. 既然WebServie也是通过HTTP进行通信的,能不使用HTTPWatch来获取它的请求过程 ...
- 用windows命令解压chm文件
Windows里有这样一个工具:hh.exe.hh.exe最重要的功能就是用来关联CHM文件,当你运行一个chm文件的时候,系统就是用这个工具来打开的. 其实它还有另外一个功能——解压CHM文件在CM ...
- k8s(openshift) 部署istio1.1
准备工作: openshift 默认不允许UID为0的容器运行,要先授权scc以便安装istio # oc adm policy add-scc-to-user anyuid -z istio-ing ...
- Unknown command: crawl
Use "scrapy" to see available commands 1.使用命令行方式cmd,是因为没有cd到项目的根目录,crawl会去搜索cmd目录下的scrapy. ...
- PHP拓展 - xhprof性能分析工具
Windows安装 参考:https://www.cnblogs.com/buexplain/p/4821619.html dll文件下载:https://windows.php.net/downlo ...
- ACdream 1108(莫队)
题目链接 The kth number Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) ...
- IoT: 物联网安全测试经验总结
前言 今年早些时候,我参与了许多关于物联网解决方案的安全测试.主要目标是找出体系结构和解决方案中的漏洞.在这篇文章中,我将讨论一些与物联网解决方案的问题和挑战. 什么是物联网? 在你学习有关IPv6的 ...
- spring boot定时任务解析
在SpringBoot中定时任务一般使用的是@Scheduled注解. @Scheduled 1.注解内容: @Target({ElementType.METHOD, ElementType.ANNO ...