leetcode Question 39: Jump Game II

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


Analysis:
Using greedy idea, every time get the max length. Details see the code.


Code (C++):


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


Code (Python):


class Solution:
    # @param A, a list of integers
    # @return an integer
    def jump(self, A):
        m = 0
        i = 0
        res = 0
        if (len(A)<=1):
            return 0
        while i<len(A):
            m = max(m,A[i]+i)
            if m>0:
                res=res+1
            if m>=len(A)-1:
                return res
            tmp=0
            for j in xrange(i+1,m+1):
                if (j+A[j]>tmp):
                    tmp = j+A[j]
                    i = j
        return res
            
                
            
        

6 comments:

  1. But this solution may exceed the time limit. Here I provide a solution using DP
    int jump(int A[], int n) {
    int *jumps=new int[n];
    int i,j;

    if(n==0 || A[0]==0)
    return 0;

    jumps[0]=0;

    for(i=1;i<n;i++)
    {
    jumps[i]=INT_MAX;
    for(j=0;j<i;j++)
    {
    if(i<=j+A[j] && jumps[j]!=INT_MAX)
    {
    jumps[i]=jumps[j]+1;
    break;
    }
    }
    }
    return jumps[n-1];
    }

    ReplyDelete
    Replies
    1. I think the time complexity of your code is the same as the above

      Delete
    2. will this DP solution be accepted on leetcode judge... I think it will give TLE. Please explain the time complexity..

      Delete
  2. Have you considered such a case: whether you can reach the last index as the case in Jump Game I?

    ReplyDelete
    Replies
    1. Do you mean I should check if there exist the solution or not ?

      I assume that the problem has at least one solution.

      To check the solution, I think simply check "if previous i == j" after the inner loop will perform well.


      Thanks.

      Delete
    2. If no solution exits, adding this line inside while loop solves the issue.

      if(m==0) return -1;

      BTW Yu Zhu, you're Awesome :)

      Delete