题解 | #跳跃游戏(三)#

跳跃游戏(三)

https://www.nowcoder.com/practice/14abdfaf0ec4419cbc722decc709938b

import java.util.*;

/**
 * NC205 跳跃游戏(三)
 * @author d3y1
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */
    public int minJumpStep (int[] nums) {
        return solution1(nums);
        // return solution2(nums);
    }

    /**
     * 动态规划
     *
     * dp[i]表示到达位置num[i]处需跳的最少次数
     *
     * dp[j] = dp[i]+1  , i+1<=j<=i+nums[i] && dp[j]=-1
     *
     * @param nums
     * @return
     */
    private int solution1(int[] nums){
        int n = nums.length;
        if(n == 0){
            return -1;
        }
        if(n == 1){
            return 0;
        }

        int[] dp = new int[n];
        Arrays.fill(dp, -1);
        dp[0] = 0;

        for(int i=0; i<n; i++){
            // 当前位置不可达
            if(dp[i] == -1){
                break;
            }
            // 最后位置可到达
            if(i+nums[i] >= n-1){
                dp[n-1] = dp[i]+1;
                break;
            }else{
                for(int j=i+1; j<=i+nums[i]; j++){
                    // 位置首次可达 次数必然最小
                    if(dp[j] == -1){
                        dp[j] = dp[i]+1;
                    }
                }
            }
        }

        return dp[n-1];
    }

    /**
     * 贪心
     * @param nums
     * @return
     */
    private int solution2(int[] nums){
        int n = nums.length;
        if(n == 0){
            return -1;
        }
        if(n == 1){
            return 0;
        }

        // 上一跳可达最大位置
        int end = 0;
        // 再一跳可达最大位置
        int pos = 0;
        // 已跳次数
        int step = 0;
        for(int i=0; i<n-1; i++){
            // 当前位置不可达
            if(i > pos){
                return -1;
            }
            pos = Math.max(pos, i+nums[i]);
            // 已达最大位置 需跳一次
            if(i == end){
                end = pos;
                step++;
            }
        }

        if(pos < n-1){
            return -1;
        }

        return step;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
07-05 16:23
门头沟学院 Java
mengnankk:我投了300,约了5 6个面试。感觉项目写的太多了。一个项目就写五六个亮点,不是把整个项目的功能描述下。其他的没啥,简历看起来有点长
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 14:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务