[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.)
| ... | i-1 | i |
| ... | 3 | 2 |
这个可以这么思考,如果i 是从i-1 跳到的,那么i位置的最少跳数 <i> = <i-1> +1.
- 创建等长的字符串a,a[i] 表示到i-th 的最少跳数,有a[0]等于0,结果就是a[n-1]。
- 创建最大已知位置索引maxIdx,表示在i-th 位置时,已经最远确定的位置索引,初始化为1,即指向未知的位置最小的。
- 遍历数组。
- 如果遍历位置加上跳跃长度大于等于maxIdx,那么更新数组a 到最大索引maxIdx
- 如果maxIdx ==n 跳出
- 返回a[n-1].
这样遍历的时间其实只有n 次,时间O(n),空间也是O(n).改进地方就是空间改O(1)。
#include <iostream>
using namespace std; class Solution {
public:
int jump(int A[], int n) {
int *a = new int [n];
for(int i=;i<n;i++)
a[i]=INT_MAX;
a[]=;
int maxidx = ;
for(int i=;i<n;i++){
while(maxidx<=i+A[i]&&maxidx<n){
// for(int kk=0;kk<n;kk++) cout<<a[kk]<<" ";
// cout<<endl;
a[maxidx++] = a[i]+;
}
}
int ret = a[n-];
delete []a;
return ret;
}
}; int main()
{
int a[]={,,,,};
Solution sol;
cout<<sol.jump(a,sizeof(a)/sizeof(int))<<endl;
return ;
}
class Solution {
public:
int jump(int A[], int n) {
int ret = ;
int last = ;
int curr = ;
for (int i = ; i < n; ++i) {
if (i > last) {
last = curr;
++ret;
}
curr = max(curr, i+A[i]);
}
return ret;
}
};
[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
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(贪婪算法)
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 ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- 二十六、MySQL 临时表
MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如 ...
- JAVA解析XML有哪几种方法?并简述各自的优缺点
DOM: 是用与平台和语言无关的方式表示XML文档的官方W3C标准,分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.是基于信息层次的 优点有:由于树在内存中是持久的,因此可以修改它以 ...
- 理解 Objective-c "属性"
理解 Objective-c "属性" @property 是OC中能够快速定义一个属性的关键字,如下我们定义一个属性. @property NSString *String; 这 ...
- Roads in the North POJ - 2631
Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...
- kuangbin 最短路集合
Til the Cows Come Home poj-2387 #include<iostream> #include<cstdio> #include<algorith ...
- 动态规划:HDU2571-命运
解题心得: 1.其实是一个简单的动态规划加上贪心的思想,思路简单,只需要求每一步的最大值就可以了,但是要注意读懂题. 2.走的规则:从左上角开始走,达到右下角,只能向右走一步,或者向下走一步,或者走列 ...
- (洛谷)P1019 单词接龙
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙" ...
- UVA 10859 Placing Lamppost 树形DP+二目标最优解的求解方案
题意:给定一个无向,无环,无多重边,要求找出最少的若干点,使得,每条边之中至少有一个点上有街灯.在满足上述条件的时候将还需要满足让两个点被选择的边的数量尽量多. 题解: 对于如何求解最小的节点数目这点 ...
- 菜鸟学Linux - 用户与用户组基础
/etc/passwd: 用户的信息是保存在/etc/passwd下面(早期的时候,用户的密码也是放在该文件中.后来出于安全考虑,将密码放在/etc/shadow中去): /etc/group: 用户 ...
- MySQL之体系结构与存储实例
定义数据库和实例 在数据库领域中有两个词很容易混淆,这就是“数据库”(database)和“实例”(instance).作为常见的数据库术语,这两个词的定义如下: 数据库:物理操作系统文件或其他形式文 ...