【LeetCode】45. 跳跃游戏 II - 贪婪算法

目录标题

  • 2023-8-11 09:49:25

45. 跳跃游戏 II

2023-8-11 09:49:25

自己没做出来,废物Orz

class Solution {
    public int jump(int[] nums) {
        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]);
            if (i == end) {
                end = maxPosition;
                steps++;
            }
        }
        return steps;
    }
}

贪婪算法思想:
【LeetCode】45. 跳跃游戏 II - 贪婪算法_第1张图片

你可能感兴趣的:(#,LeetCode,算法,leetcode)