## (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;
}
}

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;
}
};

__ATA.cmd.push(function() {
__ATA.initVideoSlot('atatags-370373-5c9102f983bca', {
sectionId: '370373',
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-26942-5c9102f983c0a',  {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160-5c9102f983c0d',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});

Related
```

1. rich
2. hujuyun