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

Thoughts:
We use the similar approach as in Jump Game with the exception that table[i] now stores the minimum step it takes from position i to the last index. We update the table from end to start. At each position i, we look at every possible position j that one can reach from i, and then pick the least step it takes from j to the last index (i.e., table[j]). We then store table[j] + 1 in table[i]. When A[i] == 0, i.e., we cannot go anywhere from position i, we make table[i] = INT_MAX. Also when we cannot get to any intermediate position that will lead us to the last index, we make table[i] = INT_MAX.

Code (Java):

public class Solution {
    public int jump(int[] A) {
        int n = A.length;
        int[] table = new int[n];
        table[n-1] = 0;
        for(int start = n - 2; start >= 0; --start) {
            if(A[start] == 0) {
                table[start] = Integer.MAX_VALUE;
            } else if(n - 1 - start <= A[start]) {
                table[start] = 1;
            } else {
                int min = Integer.MAX_VALUE;
                for(int j = start + 1; j < n && 
                    j - start <= A[start]; ++j) {
                    if(table[j] < min) {
                        min = table[j];
                    }
                }
                table[start] = min == Integer.MAX_VALUE ? 
                                min : min + 1;
            }
        }
        return table[0];   
    }
}

Code (C++):

class Solution {
public:
    int jump(int A[], int n) {
        int *table = new int[n];
        table[n-1] = 0;
        
        for (int i = n-2; i >=0; i--) {
            if (A[i] == 0)
                table[i] = INT_MAX;
            else if (A[i] >= n - i - 1)
                table[i] = 1;
            else {
                int min = INT_MAX;
                for (int j = i+1; j < n && 
                    j <= A[i] + i; j++) {
                    if (min > table[j])
                        min = table[j];
                }     
                if (min != INT_MAX)
                    table[i] = min + 1;
                else
                    table[i] = min;
            }
        }
        return table[0];  
    }
};
Advertisements

3 Comments (+add yours?)

  1. rich
    Jul 26, 2012 @ 11:34:24

    how do you format your code like that?

    Reply

  2. hujuyun
    Jan 20, 2013 @ 15:47:35

    我试了下你这个,large data还是超时了

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: