【LeetCode & 剑指offer刷题】动态规划与贪婪法题12:Jump Game(系列)

【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

Jump Game(系列)

Jump Game
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.
Determine if you are able to reach the last index.
Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last index.

C++
 
/*
问题:数组按值跳到终点(有点像大富翁,不过这里数值只是限制了最大值)
有一个非负整数的数组,每个数字表示在当前位置的基础上最多可以走的步数,求判断能不能到达最后一个位置
*/
 
/*
方法一:动态规划
dp[i]表示到达位置i时,之前的跳力还可以走的步数
状态转移方程:dp[i] = max(dp[i-1], nums[i-1]) - 1
初始值dp[0] = 0, nums[i-1]表示i-1位置处的跳力,dp[i]取决于前一个数num[i-1],也取决于前一个数前面的数dp[i-1]
如果当某一个时刻dp数组的值为负了,说明无法抵达当前位置,则直接返回false
*/
class Solution
{
public:
    bool canJump(vector<int>& nums)
    {
        vector<int> dp(nums.size()); //初始化为0,dp[0] = 0;
       
        for (int i = 1; i < nums.size(); i++)
        {
            dp[i] = max(dp[i-1], nums[i-1]) - 1;
            if (dp[i] < 0) return false;
        }
       
        return true;
    }
};
 
/*
方法二:贪心算法
这题最好的解法不是DP,而是贪婪算法Greedy Algorithm,因为我们并不是很关心每一个位置上的剩余步数,我们只希望知道能否到达末尾
 
维护一个变量maxreach,表示最远能到达的位置,初始化为0
从索引为0的数开始扫描,计算最大maxreach(即i+nums[i]的最大值),如果最后的maxreach大于终点的索引,则说明可以从起始点跳到终点
*/
class Solution
{
public:
    bool canJump(vector<int>& nums)
    {
        if(nums.empty())
            return false;
       
        int maxreach = 0;
        for(int i = 0; i< nums.size();i++)
        {
            maxreach = max(maxreach, i+nums[i]);
           
            if(i+1 < nums.size() && maxreach < i+1)//若当前跳力无法跳到下一个位置,返回false
                return false;
        }
        return true;
    }
};
 
 
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.

 
/*
问题:跳跃游戏(求跳到末尾最少的步数)
方法:贪婪法
我们这里贪的是一个能到达的最远范围,我们遍历当前能跳到的位置,然后根据该位置上的跳力来预测下一步能跳到的最远距离贪出一个最远的范围,一旦当这个范围到达末尾时,当前所用的步数一定是最小步数
(选取各步中可以跳到的最远位置进行接力)
 
将游戏看成一种接力赛,用cur和next来接力
*/
class Solution
{
public:
    int jump(vector<int>& nums)
    {
        if(nums.empty()) return -1;
       
        int res = 0, n = nums.size();
        int cur = 0, next = 0; //cur和next分别表示当前和之后能到达的最远位置
        int i = 0; //位置索引
        while (next < n - 1)
        {
            cur = next;
            for (; i <= cur; i++) //遍历到当前能跳到的位置
            {
                next = max(next, i + nums[i]);//找区间[prev,cur]中最大的跳力,更新之后能跳到的最远位置
            }
            if (next == cur) 
                return -1; // 如果相等说明next没有更新,即无法越过cur,返回-1
            else
                res++; //跳的步数加1(一个for循环,next的更新代表一次跳跃
        }//退出循环时,next >= n-1,代表最后一次跳跃
        return res;
    }
};
 
原文地址:https://www.cnblogs.com/wikiwen/p/10229382.html