leetcode Question 38: 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.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.


Analysis:

Note that the A[i] means the maximum jump length. In other words, it is possible that we move j step (<A[i]) when we at A[i], e.g. if A[i]=2, we can move either 1 or 2 steps.

The simple but time-consuming way is O(n^2). Set every future step true according to the A[i]. But this only can pass the small test.

A better idea is use another variable to save the number of steps available. Just a little bit different from the O(n^2) one , see code for more details.



Source code: (C++ : Small)

class Solution {
public:
    bool canJump(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (n==0) {return false;}
        if (n==1) {return true;}
        
        vector<bool>B(n-1,false);
        B[0]=true;
        for (int i=0;i<n;i++){
            if (B[i]==true){
                for (int j=1;j<=A[i];j++){
                    B[i+j]=true;
                }
            }
        }
        return B[n-1];
    }
};

Source code: (C++ : DP)


class Solution {
public:
    bool canJump(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (n==0) {return false;}
        if (n==1) {return true;}
        
        int m=0;
        for (int i=0;i<n;i++){
            if (i<=m){
             m=max(m,A[i]+i);
             if (m>=n-1){return true;}
            }
        }
        return false;
    }
};


Code (Python):


class Solution:
    # @param A, a list of integers
    # @return a boolean
    def canJump(self, A):
        m = 0
        for i in xrange(0,len(A)):
            if i<=m:
                m = max(A[i]+i,m)
                if m>=len(A)-1:
                    return True
        return False
            
            
        

5 comments:

  1. Great post! but i+j could be out of range in your first approach. One small modification would make your 1st approach pass the large set.
    public static boolean canJump1(int[] A) {
    // Start typing your Java solution below
    // DO NOT write main() function
    int len=A.length;
    if(len==0) return false;
    if(len==1) return true;
    boolean[] dp=new boolean[A.length];
    dp[0]=true;
    for(int i=0;i<len;i++){
    if(dp[i]){
    int c=A[i];
    for(int j=1;j<=c;j++){
    if(i+j<len){
    dp[i+j]=true;
    }else{
    return true;
    }
    }
    }
    }
    return dp[len-1];

    }

    ReplyDelete
    Replies
    1. Oh yes! Your code is right!

      I forgot to consider the bound range.

      Thanks for the reply!


      Delete
  2. class Solution {
    public:
    bool canJump(int A[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if (n==0) {return false;}
    if (n==1) {return true;}

    int m=0;
    for (int i=0;i=n-1){return true;}
    }
    }
    return false;
    }
    };

    why you need
    vectorB(n-1,false);
    B[0]=true;

    in second solution?

    ReplyDelete
  3. what about below solution:

    class Solution {
    public:
    bool canJump(int A[], int n) {
    if(!n) return false;
    int target = n-1;
    for(int i = n-2;i>=0;i--){
    if((target-i)<=A[i]){target = i;}
    }
    if(target > 0) return false;
    return true;
    }
    };

    will it pass all scenarios?

    ReplyDelete