贪心算法:跳跃游戏
跳跃问题1:leetcode55
关键点:求最大距离是否能大于最后一个位置。
bool canJump(vector<int>& nums) {
int n = nums.size();
int farthest = 0;
for (int i = 0; i < n - 1; i++) {
// 不断计算能跳到的最远距离
farthest = max(farthest, i + nums[i]);
// 可能碰到了 0,卡住跳不动了
if (farthest <= i) return false;
}
return farthest >= n - 1;
}
跳跃游戏2题目:leetcode45
贪心算法:每次选择一个能达到最大范围的值,在下面的范围中再进行选择。
public static int tx(int[] nums,int p){
int n = nums.length;
int end = 0,farthest = 0;
int jumps = 0;
for (int i=0;i<n-1;i++){
farthest = Math.max(nums[i]+i,farthest);
if (end == i){
jumps++;
end = farthest;
}
}
return jumps;
}