leetcode Brush question record--Greedy Algorithm--55. Jumping game

leetcode Brush question record

Greedy Algorithm

Core concept:

Step 1: start from an initial solution;

Step 2: adopt the iterative process. When you can move one step towards the goal, get a decomposition according to the local optimal strategy to reduce the scale of the problem;

Step 3: synthesize all solutions.

55. Jumping game

Given a nonnegative integer array nums, you are initially at the first subscript of the array.Each element in the array represents the maximum length you can jump at that position.Judge whether you can reach the last subscript.

Example 1:

Input: num = [2,3,1,1,4]

Output: true

Explanation:

you can jump 1 step first, from subscript 0 to subscript 1, and then jump 3 steps from subscript 1 to the last subscript.

Example 2:

Input: num = [3,2,1,0,4]

Output: false

Explanation:

in any case, you will always reach the position with subscript 3. However, the maximum jump length of this subscript is 0, so it is never possible to reach the last subscript.

Official explanation:

We can solve this problem in a greedy way.

Imagine, for any position YY in the array, how can we judge whether it can be reached? According to the description of the topic, as long as there is a position XX, it can be reached by itself, and the maximum length of its jump is x + \ textit {nums} [x] x + nums [x], which is greater than or equal to YY, that is, x + \ textit {nums} [x] \ GEQ YX + nums [x] ≥ y, then the position YY can also be reached.

In other words, for each reachable position XX, it makes x + 1, x + 2, \ cdots, x + \ textit{nums} [x] x + 1, x + 2,…, x + nums [x] these continuous positions reachable.

In this way, we traverse each position in the array in turn and maintain the farthest reachable position in real time. For the currently traversed position XX, if it is within the range of the farthest reachable position, we can reach the position through several jumps from the starting point, so we can update the farthest reachable position with x + \ textit{nums} [x] x + nums [x].

In the process of traversal, if the farthest reachable position is greater than or equal to the last position in the array, it means that the last position is reachable, and we can directly return true as the answer. On the contrary, if the last position is still unreachable after traversal, we will return false as the answer.

Take example 1 in the title

[2, 3, 1, 1, 4]

For example:

At the beginning, at position 00, the maximum length we can jump is 22, so the farthest reachable position is updated to 22;

We traverse to position 11. Since 1 \ Leq 21 ≤ 2, position 11 can be reached. We use 11 plus the maximum length 33 it can jump to update the farthest reachable position to 44. Since 44 is greater than or equal to the last position 44, we directly return true.

Let’s take another look at example 2 in the title

[3, 2, 1, 0, 4]

At the beginning, at position 00, the maximum length we can jump is 33, so the farthest reachable position is updated to 33;

We traverse to position 11. Since 1 \ Leq 31 ≤ 3, position 11 can be reached. Plus the maximum length 22 it can jump, we get 33, which does not exceed the farthest reachable position;

Position 22 and position 33 are the same, and the farthest reachable position will not be updated;

We traverse to position 44. Since 4 > 34 > 3, position 44 is unreachable, so we don’t consider the maximum length it can jump.

After the traversal, position 44 is still unreachable, so we return false.

class Solution {
    public boolean canJump(int[] nums) {
        int n = nums.length;
        int rightmost = 0;
        for (int i = 0; i < n; ++i) {
            if (i <= rightmost) {
                rightmost = Math.max(rightmost, i + nums[i]);
                if (rightmost >= n - 1) {
                    return true;
                }
            }
        }
        return false;
    }
}

Solution to the problem of hanging and beating the official boss

class Solution {
    public boolean canJump(int[] nums) {
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i > k) return false;
            k = Math.max(k, i + nums[i]);
        }
        return true;
    }
}

ps:

Problem solving ideas:

If the jumping distance of a certain grid as the take-off point is 3, it means that the following three grids can be used as the take-off point

You can try to jump once for each grid that can be used as the take-off point, and constantly update the farthest distance you can jump

If you can jump to the end, you will succeed

Author: ikaruga

Link: https://leetcode-cn.com/problems/jump-game/solution/55-by-ikaruga/

Source: leetcode

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值