LeetCode:Jump Game I II


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.

 

算法1:暴力解法,注意A[0] = 0的邊界條件.該解法O(n^2),大數據超時了

class Solution {
public:
    bool canJump(int A[], int n) {
        if(n == 1)return true;
        else if(A[0] == 0)return false;
        bool canArrive[n];
        memset(canArrive, 0, sizeof(canArrive));
        canArrive[0] = true;
        for(int i = 0; i < n; i++)
        {
            if(canArrive[i] == false)continue;
            int farest = min(i + A[i], n - 1);
            for(int j = i + 1; j <= farest; j++)
                canArrive[j] = true;
            if(canArrive[n-1])return true;
        }
        return canArrive[n-1];
    }
};

 

算法2:優化解法,只需要順序掃描數組,記錄下能夠到達的最遠位置

class Solution {
public:
    bool canJump(int A[], int n) {
        int canArrive = 0;//當前能到達的最遠位置
        for(int i = 0; i <= canArrive && canArrive < n-1; i++)
            if(i + A[i] > canArrive)canArrive = i + A[i];
        return canArrive >= n-1;
    }
};

 


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.

For example:
Given array A = [2,3,1,1,4]

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

 

算法3:在上述算法1的基礎上(其實是動態規划,minjumps[i] = min{minjumps[k] + 1},k<i 且 i+A[k]>=i )                            本文地址

class Solution {
public:
    int jump(int A[], int n) {
        vector<int> minjumps(n, INT_MAX);
        minjumps[0] = 0;
        for(int i = 0; i < n; i++)
        {
            int farest = min(i + A[i], n - 1);
            for(int j = i + 1; j <= farest; j++)
                if(minjumps[j] > minjumps[i] + 1)
                    minjumps[j] = minjumps[i] + 1;
        }
        return minjumps[n-1];
    }
};

 

算法4:在上述算法2的基礎上(具體解釋可參考http://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html

class Solution {
public:
    int jump(int A[], int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        int canArrive = 0, res = 0, lastCanArrive = 0;
        for(int i = 0;  i < n; i++)
        {
            if(i > lastCanArrive)
            {
                res++;
                lastCanArrive = canArrive;
            }
            if(i + A[i] > canArrive)
                canArrive = i + A[i];
        }
        return res;
    }
};

 

稍微改進一下,只要canArrive >= n-1 ,就可以結束循環,此時返回值是res+1

class Solution {
public:
    int jump(int A[], int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(n == 1)return 0;
        int canArrive = 0, res = 0, lastCanArrive = 0;
        for(int i = 0;  canArrive < n-1; i++)
             if(i + A[i] > canArrive)
             {
                 if(i > lastCanArrive)
                 {
                     res++;
                     lastCanArrive = canArrive;
                 }
                 canArrive = i + A[i];
             }
        return res+1;
    }
};

 

算法5:從最后一個開始,找到第一個能到最后的,再往前找第一個能到新的位置的,直到第0位(參考http://www.laurashawn.net/?p=10885

class Solution {
    public:
    int jump(int A[], int n) {
        int i=n-1;
        int step=0;
        while(i>0){
            for(int j=0;j<i;j++){
                if(A[j]+j>=i){
                    step++;
                    i=j;
                    break;
                }
            }
        }
        return step;
    }
};

 

 

【版權聲明】轉載請注明出處:http://www.cnblogs.com/TenosDoIt/p/3719630.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM