1. 程式人生 > >LeetCode 45. 55. Jump Game

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