Introducing the big factory series "Jumping Game Triple Combo"

Get into the habit of writing together! This is the 15th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

1. Jumping Game I

Given an array nums of non-negative integers, you are initially at the first .

Each element in the array represents the maximum length you can jump at that position.

Determine if you can reach the last subscript.

Example 1 :

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

Example 2 :

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

leetcode

1. Analysis

The villain wants to jump from position 0 to N-1position, the value in the array represents the maximum number of steps that can be jumped

Array subscript conversion problem, set a variable max, representing the maximum position that can jump to the right

2. Realize

public static boolean canJump(int[] nums) {
    if (nums == null || nums.length < 2) {
        return true;
    }
    // max代表能跳到右侧的最大位置
    int max = nums[0];
    for (int i = 1; i < nums.length; i++) {
        if (i > max) {
            return false;
        }
        max = Math.max(max, i + nums[i]);
    }
    return true;
}
复制代码

2. Jumping Game II

Given an array nums of non-negative integers, you are initially at the first position of the array.

Each element in the array represents the maximum length you can jump at that position.

Your goal is to get to the last position of the array using the fewest number of jumps.

Assuming you can always reach the last position of the array.

Example 1 :

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

Example 2 :

输入: nums = [2,3,0,1,4]
输出: 2
复制代码

leetcode

1. Analysis

Number of jumping steps: It is not the meaning of jumping one step, such as the meaning of the farthest jump to within 1 step.

Set three variables, step number of jumping steps, cur which is the farthest within the current number of jumping steps, next which is the farthest jumping step in the next step

A few steps (step) can reach the rightmost position (cur), and one more jump can reach the rightmost position (next)

The initial values ​​of step, cur, and next are as follows:

0 step jump step=0, can reach the rightmost position cur=0, can reach the rightmost position in one step next=arr[0]

  • step: the current minimum number of jump steps to reach i position
  • cur: The number of steps to jump does not exceed step, which position can the far right go to
  • next: The number of steps to jump does not exceed step+1, which position can the far right go to

i > cur, indicating that the step step is not enough to reach i, step++, increase the number of steps (the number of jumps), cur = next, to put it bluntly, next is prepared in advance. In order to cover the next step, next is given to cur

i ≤ cur, it means that i can still reach i within the step step, and it can be covered. See if you can update next (next = max(next, i+arr[i])), next must always pay attention to whether it can go further.

2. Realize

public static int jump(int[] arr) {
    if (arr == null || arr.length == 0) {
        return 0;
    }
    int step = 0;
    int cur = 0;
    int next = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (cur < i) {
            step++;
            cur = next;
        }
        next = Math.max(next, i + arr[i]);
    }
    return step;
}
复制代码

3. Jumping Game III

Here is an array arr of non-negative integers, and you start at the start index start of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i].

Please judge whether you can jump to any subscript whose corresponding element value is 0.

Note that you cannot jump outside the array under any circumstances.

Example 1 :

输入:arr = [4,2,3,0,3,1,2], start = 5
输出:true
解释:
到达值为 0 的下标 3 有以下可能方案: 
下标 5 -> 下标 4 -> 下标 1 -> 下标 3 
下标 5 -> 下标 6 -> 下标 4 -> 下标 1 -> 下标 3
复制代码

Example 2 :

输入:arr = [4,2,3,0,3,1,2], start = 0
输出:true 
解释:
到达值为 0 的下标 3 有以下可能方案: 
下标 0 -> 下标 4 -> 下标 1 -> 下标 3
复制代码

Example 3 :

输入:arr = [3,0,2,1,2], start = 2
输出:false
解释:无法到达值为 0 的下标 1 处。 
复制代码

leetcode

1. Analysis

At the current position i, there are only two choices, jump left or jump right, similar to a binary tree.

The role of breadth-first traversal (queue): binary tree, two branches, you can go to the left (left) and to the right (right)

  • key: array subscript, value: number of layers
  • De-duplication, record past decisions

2. Realize

public static boolean canReach(int[] arr, int start) {
    if (arr == null || start < 0 || start > arr.length - 1) {
        return false;
    }
    // 通过队列实现宽度优先遍历
    Queue<Integer> queue = new LinkedList<>();
    // 按层遍历
    HashMap<Integer, Integer> levelMap = new HashMap<>();
    queue.add(start);
    levelMap.put(start, 0);
    while (!queue.isEmpty()) {
        int cur = queue.poll();
        int level = levelMap.get(cur);
        if (cur + arr[cur] < arr.length && arr[cur + arr[cur]] == 0) {
            return true;
        } else {
            int left = cur - arr[cur];
            if (left >= 0 && !levelMap.containsKey(left)) {
                queue.add(left);
                levelMap.put(left, level + 1);
            }
            int right = cur + arr[cur];
            if (right < arr.length && !levelMap.containsKey(right)) {
                queue.add(right);
                levelMap.put(right, level + 1);
            }
        }
    }
    return false;
}
复制代码

Guess you like

Origin juejin.im/post/7086810447529115679