2022年3月26日 作者 zeroheart

dp第四天

今天这个是用的贪心,不知道算不算dp。。

55. 跳跃游戏

/**
55. 跳跃游戏
给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

判断你是否能够到达最后一个下标。



示例 1:

输入:nums = [2,3,1,1,4]
输出:true
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
示例 2:

输入:nums = [3,2,1,0,4]
输出:false
解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。


提示:

1 <= nums.length <= 3 * 10^4
0 <= nums[i] <= 10^5

* @author zeroheart
*
*/
public class Question55 {
public boolean canJump(int[] nums) {
int len = nums.length;
if(len <= 1)return true;

// 当前最远能跳到哪
int max = nums[0];
for(int i = 0; i<len; i++){
if(max >= len - 1) return true;
if(max >= i){
// 首先要能跳到i,才有下面的计算
max = Math.max(max, i + nums[i]);
}
}
return false;
}

public static void main(String[] args) {

}
}

有两个注意点,一个if(max >= len – 1) return true;另一个是if(max >= i)才进入max比较

45. 跳跃游戏 II

这题要注意的点就是,不是每次都跳最远的那个,更新步数的地方是特殊的。比较麻烦的,看下图解吧,图解很简单。。

跳跃游戏 II – 跳跃游戏 II – 力扣(LeetCode) (leetcode-cn.com)


/**
 45. 跳跃游戏 II
 给你一个非负整数数组 nums ,你最初位于数组的第一个位置。

 数组中的每个元素代表你在该位置可以跳跃的最大长度。

 你的目标是使用最少的跳跃次数到达数组的最后一个位置。

 假设你总是可以到达数组的最后一个位置。



 示例 1:

 输入: nums = [2,3,1,1,4]
 输出: 2
 解释: 跳到最后一个位置的最小跳跃数是 2。
 从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
 示例 2:

 输入: nums = [2,3,0,1,4]
 输出: 2


 提示:

 1 <= nums.length <= 10^4
 0 <= nums[i] <= 1000

 * @author zeroheart
 *
 */
public class Question45 {
    public int jump(int[] nums) {
//        int len = nums.length;
//
//        if(len <= 1) return len;
//
//        int max = nums[0];
//        int times = 1;
//
//        for(int i = 1; i<len; i++){
//            if(max >= len - 1) return times;
//            if(max >= i){
//                max = Math.max(max, i+nums[i]);
//                times++;
//            }
//        }
//        return 0;

        // 上面的写法没有考虑不是每次都要跳到最多的,可能会错过了最大的,和贪心第一题的区别在与第一题只要能跳到就行了,不管你跳了多少次。

        int length = nums.length;
        int end = 0;
        int maxPosition = 0;
        int steps = 0;
        for (int i = 0; i < length - 1; i++) {
            maxPosition = Math.max(maxPosition, i + nums[i]);
            // 只有下标到达了这一次步的最远,才会更新,但是实际上不一定是这么跳的,max可能已经跳过去了
            if (i == end) {
                end = maxPosition;
                steps++;
            }
        }
        return steps;

    }


    public static void main(String[] args) {

    }
}