zoukankan      html  css  js  c++  java
  • 45. Jump Game II

    问题描述:

     

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

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

    Your goal is to reach the last index in the minimum number of jumps.

    Example:

    Input: [2,3,1,1,4]
    Output: 2
    Explanation: The minimum number of jumps to reach the last index is 2.
        Jump 1 step from index 0 to 1, then 3 steps to the last index.

    Note:

    You can assume that you can always reach the last index.

    解题思路:

    看到求最小的步数,于是我就用了dp,天真如我,超时了:)

    记录一下dp的分析吧

    可以用一维dp数组

    dp[i]代表从起始点到i点的最小距离。

    对每一个元素,我们对它能够到达的点的dp进行更新:

           for(int j = 1; j <= nums[i]; j++){
                    if(i+j >= nums.size())
                        break;
                    dp[i+j] = min(dp[i]+1, dp[i+j]);
                }

    最后求dp[nums.size()]

    然后就超时了,然后我打开相关标签一看,居然是greedy啊我的朋友们!!!!

    代码:

    dp:

    class Solution {
    public:
        int jump(vector<int>& nums) {
            if(nums.size() < 2)
                return 0;
            vector<int> dp(nums.size(), INT_MAX);
            dp[0] = 0;
            for(int i = 0; i < nums.size(); i++){
                for(int j = 1; j <= nums[i]; j++){
                    if(i+j >= nums.size())
                        break;
                    dp[i+j] = min(dp[i]+1, dp[i+j]);
                }
            }
            return dp[nums.size()-1];
        }
    };

    greedy:参考链接图文解释

    朋友们,神一样的解法啊!!!

    O(n)的复杂度

    每次cur是代表最大能到的范围,last代表上一次能到的范围,每当i > last 时,要更新可达范围,以为已经跳出了上一次能走的最大范围

    此时也要更新last到本次能达到的可及范围,同时增加ret。

    class Solution {
    public:
        int jump(vector<int>& nums) {
            int ret = 0;
            int last = 0;
            int cur = 0;
            for(int i = 0; i < nums.size(); i++){
                if(i > last){
                    last = cur;
                    ret++;
                }
                cur = max(cur, i+nums[i]);
            }
            return ret;
        }
    };
  • 相关阅读:
    Spark 集群 任务提交模式
    Spark RDD 宽窄依赖
    Spark 资源调度包 stage 类解析
    堆内内存与堆外内存
    Spark笔记(一)
    scala 语言特性
    Centos7.4 Storm2.0.0 + Zookeeper3.5.5 高可用集群搭建
    fs模块他的作用是(文件夹)
    事件循环机制
    简单的下路由(我们可以在控制器中看到路由参数)
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9165372.html
Copyright © 2011-2022 走看看