LeetCode 45. 55. Jump Game

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.

重点在于:每次跳不是一个定数,而是当前下表的最大值,同时求最少跳几次达到最后一格

本题可使用贪婪算法:
即每次跳的是,这个范围内跳的最远的那一格

  • 需要一个变量表示当前的下标
  • 一个变量表示下一步可达到的最大下标
  • 遍历当前下标表示的范围: 进行取舍下一步应该跳到哪一个下标
class Solution {
public:
    int jump(vector<int>& nums) {
        if(nums.size() <= 1)
            return 0;
        if(nums[0] == 0)
            return -1;
        //记录当前活动最大距离
        int reach = nums[0];
        int steps = 0, start = 0;
        for(; start < nums.size() && start <= reach;)
        {
            steps++;
            if(reach >= (nums.size() - 1))
            {
                return steps;
            }
            //newMax 表示下一步能到达的最远距离
            int nextMax = 0;
            //在当前start 和 reach 之间,找到下一步到达最远距离的下标
            for(int i = start; i <= reach; ++i)
            {
                if(i + nums[i] > nextMax)
                {
                    nextMax = i + nums[i];
                    start = i;
                }
            }
            reach = nextMax;
        }
        return -1;
    }
};
  • 第二种:
int canJump(vector<int>& nums) {

     if(nums.size() == 0)
     {
         return -1;
     }
     //当前能跳到的最大位置
     int cur = 0;
     int last = 0;   //指从之前的点能reach到的最远距离
     int step = 0;
     for(int i = 0; i < nums.size(); i++)
     {
         if(i > last)
         {
             step++;
             last = cur;
         }
         cur = max(cur, nums[i] + i);
     }

     return cur < nums.size() - 1 ? -1 : step;
}

55.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.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

  • 每次跳跃选择往最远处跳跃,如果最后能够跳到数组最后一位或者最后一位之后,那么一定存在一种跳跃方式正好跳到最后一位上。
  • 第一种方法 (动态规划)
    • 可以算出每一格下一步到达的最大下标
bool canJump(vector<int>& nums) {
       int n = nums.size();
       //dp[i]表示当前跳跃的最大距离
       int dp[n];
       dp[0] = nums[0];
	for (int i = 1; i < n; i++)
	{
		if(i <= dp[i - 1])
			dp[i] = max(dp[i - 1], i + nums[i]);
		else
			dp[i] = dp[i - 1];
	}               
        return dp[n - 1] >= (n - 1);
    }
  • 第二种方法 (贪心算法)
    • 维护一个当前能跳到的最大maxJump,若是maxJump已经>=num.length - 1,说明能跳到最后一个点,return true.若是过程中maxJump <= i, 说明跳到当前点便不能往前,跳出loop, return false.
bool canJump(vector<int>& nums) {
       int n = nums.size();
       //表示当前跳跃的最大距离
       int maxJump = 0; 
	for (int i = 0; i < n; i++)
	{
		if (i > maxJump || maxJump >= (n - 1))
			break;
		// nums[i]+i当前跳最远距离 maxJump为i之前跳最远距离
		maxJump = max(maxJump, i + nums[i]);
	}               
        return maxJump >= (n - 1);
    }

猜你喜欢

转载自blog.csdn.net/qq_22613757/article/details/83870934