贪心
每次跳跃的落点选择未来(下一跳)能跳最远的位置,容易证明这样的跳跃方案就是最优的。
45. 跳跃游戏 II
class Solution {
public:
int jump(vector<int>& nums) {
int tip=nums[0];
int len=nums.size();
int now=0;
int tmp,tmpx;
int ans=0;
if (len==1) ans=0;
else ans++;
while (tip<len-1){
ans++;
tmp=tip;
for (int i=now;i<=tip;i++){
if (i+nums[i]>tmp){
//选择未来(下一跳)能跳最远的位置
tmp=i+nums[i];
tmpx=i;
}
}
now=tip+1;
tip=tmp;
}
return ans;
}
};