leetcode–jump game II
1.题目描述
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.)
2.解法分析
首先判断能不能到达最后一个元素。如果能,接着分析。这里有一个动态规划的解法,不过比较耗时,思路是这样的,设置一个数组dp,dp长度与源数组长度一致,为n.其中dp[i]表示到达数组第i个元素的最短步数,那么dp[i]只跟i之前一步能够到达i的位置有关。于是有了下面的代码,小数据集直接就AC了,大数据集却卡住了,极端情况下这个算法的复杂度是O(N2),但是侥幸心理让我还是写了这个解法,但是还是没过,悲催,先记录一下吧。
class Solution {public:int jump(int A[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(n<2)return 0;set<int>onestep;vector<int> dp;dp.assign(n,0);set<int>::iterator iter;for(int i=1;i<n;++i){for(iter=onestep.begin();iter!=onestep.end();++iter){if((A[*iter]+*iter)<i)onestep.erase(*iter);}if((i-1+A[i-1])>=i)onestep.insert(i-1);int minStep=n;for(iter=onestep.begin();iter!=onestep.end();++iter){if(dp[*iter]<minStep){minStep=dp[*iter];}}dp[i]=minStep+1;}return dp[n-1];}};然后在网上发现了这么个解法,感觉豁然开朗,我一开始就被动态规划迷住了双眼,这个解法反其道而行之,很妙,思路是这样的,假设我们现在已经知道了再ret步之内能够到达的最远距离last,那么从当前位置到last,我们逐一计算它们一步之内能到达的位置,如果该位置大于last,且大于curr,那么ret+1步之内能到达的最远位置更新为这个值,继续保存在curr之中,一旦我们遍历过了last,那么说明我们需要ret+1步才能到达了,将last设置为curr.重复刚才的判断直至结束。具体的算法很简单,如下:
/** We use "last" to keep track of the maximum distance that has been reached* by using the minimum steps "ret", whereas "curr" is the maximum distance* that can be reached by using "ret+1" steps. Thus,* curr = max(i+A[i]) where 0 <= i <= last.*/class Solution {public:int jump(int A[], int n) {int ret = 0;int last = 0;int curr = 0;for (int i = 0; i < n; ++i) {if (i > last) {last = curr;++ret;}curr = max(curr, i+A[i]);}return ret;}};
总结,第一反应的算法不是好算法,要仔细想想,其实这个思路跟jump game那个差不多,只是思路更隐蔽。
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 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 ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- 我 Git 命令列表 (1)【转】
转自:http://www.microsofttranslator.com/bv.aspx?from=en&to=zh-CHS&a=http%3A%2F%2Fvincenttam.gi ...
- mmm hardware/libhardware_legacy/power/
android源码目录下的build/envsetup.sh文件,描述编译的命令 - m: Makes from the top of the tree. - mm: Buil ...
- python 捕获 shell/bash 脚本的输出结果
#!/usr/bin/python## get subprocess module import subprocess ## call date command ##p = subprocess.Po ...
- [HDOJ2512]一卡通大冒险(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2512 给一个数n,问1~n这n个数内的划分.设dp(i,j)为i划分为j个集合时有多少个. 初始化条件 ...
- 添加crontab为什么要重定向输出到/dev/null
如果crontab不重定向输出,并且crontab所执行的命令有输出内容的话,是一件非常危险的事情.因为该输出内容会以邮件的形式发送给用户,内容存储在邮件文件 /var/spool/mail/$use ...
- many to one could not resolve property
今天在做一个功能的时候 遇到了.一个Could not resolve property 的问题. 配置文件如下: <many-to-one name="user" cla ...
- Chrome 快捷键使用
窗口和标签页快捷方式 Ctrl+N 打开新窗口 按住 Ctrl 键,然后点击链接 在新标签页中打开链接 按住 Shift 键,然后点击链接 在新窗口中打开链接 Alt+F4 关闭当前窗口 Ctrl+ ...
- (六)6.17 Neurons Networks convolutional neural network(cnn)
之前所讲的图像处理都是小 patchs ,比如28*28或者36*36之类,考虑如下情形,对于一副1000*1000的图像,即106,当隐层也有106节点时,那么W(1)的数量将达到1012级别,为了 ...
- IOS设计模式之四(备忘录模式,命令模式)
本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq) ...
- 【转】gcc中-pthread和-lpthread的区别
原文网址:http://chaoslawful.iteye.com/blog/568602 用gcc编译使用了POSIX thread的程序时通常需要加额外的选项,以便使用thread-safe的库及 ...