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;
    }
};

 

posted @ 2018-06-11 08:10  妖域大都督  阅读(132)  评论(0编辑  收藏  举报