Saturday, September 14, 2013

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.
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.)
using Dynamic appaorach
int jump(int A[], int n) {
    int minJumps[1000] = {0};
    minJumps[0] = 0;
    for(int i = 1; i < n; i++)
    {
        minJumps[i] = INT_MAX;
        for(int j = 0; j < i; j++)
        {
            if(A[j] + j >= i)
            {
                minJumps[i] = min(minJumps[i], 1 + minJumps[j]);
            }
        }
    }
    
    
    if(minJumps[n-1] == INT_MAX)
    {
        return -1;
    }
    
    return minJumps[n-1];
}


 int jump(int A[], int n) {
        int step = 0;
        int start =0;
        int end = 0;
        int next = 0;
        while (end < n - 1){
            step++;
            for (int i = start; i <= end; i++)
                next = max(next, A[i] + i);
            start = end + 1;
            if (next <= end) return -1;
            end = next;
        }
        return step;
    }

No comments:

Post a Comment