个人技术分享

题目链接
在这里插入图片描述

贪心

class Solution {
    public int jump(int[] nums) {
        if(nums.length == 1){
            return 0;
        }
        int count = 0;
        // 当前覆盖最远距离下标
        int curDistance = 0;
        // 下一步覆盖距离最远下标
        int nextDistance = 0;
        for(int i = 0; i <= nums.length; i++){
            nextDistance = Math.max(nums[i] + i, nextDistance);
            // 遇到当前覆盖最远距离下标
            if(i == curDistance){
                count++;
                // 更新当前覆盖最远距离下标
                curDistance = nextDistance;
                if(nextDistance >= nums.length - 1){
                    break;
                }
            }
        }
        return count;
    }
}