LeetCode 刷題記錄 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.
解法1:
貪心法:
用left和right記錄我們現在所能達到的距離,然後遍歷更新最遠的距離,
例: [2,3,1,1,4,8,2]
初始化: left 和 right 爲0,表示我們最開始只能到達0號位
如果數組只有1個元素,我們需要特判輸出0,因爲不需要移動即可到達最後的位置
while 循環:left <= right ,如果跳出循環即left > right

  1. 跳數爲1, 現在我們能達到的位置只有0號位,遍歷後更新最遠右距離爲 0 + nums[0] = 2,最遠左距離爲以前的右距離 + 1,至少我們能前進一格,否則我們則永遠出不去
  2. 跳數爲2, 現在我們能達到的位置只有1到2號位,遍歷後更新最遠距離爲 1 + nums[1] = 4,最遠左距離爲以前的右距離 + 1,爲3
  3. 跳數爲2, 現在我們能達到的位置只有3到4號位,遍歷後更新最遠距離爲 4+ nums[4] = 8,大於數組長度我們成功跳到終點
    c++:
class Solution {
public:
    int jump(vector<int>& nums) {
        int step = 0;
        int left = 0, right = 0;
        if(nums.size() == 1) return 0;
        while(left <= right){
            step++;
            int old_right = right;
            for(int i = left; i <= old_right; ++i){
                int new_right = i + nums[i];
                if(new_right >= (nums.size() - 1)) return step;
                if(new_right > right) right = new_right;
            }
            left = old_right + 1;
        }
        return 0;
    }
};

java:

class Solution {
    public int jump(int[] nums) {
        int step = 0;
        int left = 0, right = 0;
        if(nums.length == 1) return 0;
        while(left <= right){
            step++;
            int old_right = right;
            for(int i = left; i <= old_right; ++i){
                int new_right = i + nums[i];
                if(new_right >= (nums.length - 1)) return step;
                if(new_right > right) right = new_right;
            }
            left = old_right + 1;
        }
        return 0;
    }
}

python:

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
      
        step, left , right = 0 , 0, 0
        if len(nums) == 1: return 0
        while left <= right:
            step += 1
            old_right = right
            for i in xrange(left, old_right + 1):
            
                new_right = i + nums[i]
                if new_right >= (len(nums) - 1): return step
                if new_right > right: right = new_right
            
            left = old_right + 1
        
        return 0

解法2:

解法核心是用lastFarthest標記上次能到達的最遠距離,curFarthest標記最遠距離,當我們到達上次能到達的最遠距離更新跳數
例: [2,3,1,1,4]
初始化: lastFarthest和curFarthest,表示我們上次能到達的最遠距離和現在最遠距離爲0,同時跳數也爲0
從0 遍歷到 n -2,最後是我們要跳的終點,所以不用遍歷
i = 0: curFarthest = 2,最遠能跳到2,同時也到達上次能到達的最遠距離,更新跳數爲1,現在最遠能跳到2,同時更新上次能到達的最遠距離爲2
i = 1: curFarthest = 4,最遠能跳到4
i = 2: curFarthest = 4,最遠能跳到4,同時也到達上次能到達的最遠距離,更新跳數爲2,現在最遠能跳到4,到達終點結束

c++:

class Solution {
public:
    int jump(vector<int>& nums) {
        int step = 0;
        int lastFarthest = 0;
        int curFarthest = 0;
        int n = nums.size();
        for(int i = 0; i < (n -1); ++i){
            curFarthest = max(i + nums[i], curFarthest);
            if(i == lastFarthest){
                step++;
                if(curFarthest >= (n-1)) break;
                lastFarthest = curFarthest;
            }
        }
        return step;
    }
};

java:

class Solution {
    public int jump(int[] nums) {
        int step = 0;
        int lastFarthest = 0;
        int curFarthest = 0;
        int n = nums.length;
        for(int i = 0; i < (n -1); ++i){
            curFarthest = Math.max(i + nums[i], curFarthest);
            if(i == lastFarthest){
                step++;
                if(curFarthest >= (n-1)) break;
                lastFarthest = curFarthest;
            }
        }
        return step;
    }
}

python:

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
      
        step, lastFarthest , curFarthest = 0 , 0, 0
        
        n = len(nums)
        for i in xrange(n-1):
            curFarthest = max(i + nums[i], curFarthest)
            if i == lastFarthest:
                step += 1
                if curFarthest >= (n-1): break
                lastFarthest = curFarthest
            
        
        return step
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章