## [LeetCode] 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.)

```class Solution {
public:
int jump(vector<int>& nums) {
int len = nums.size();
if(len<2){
return 0;
}
int d[len];
d[0] = 0;
vector<int> pre[len];
for(int i=1; i<=nums[0] && i<len; i++){
pre[i].push_back(0);
}
for(int i=1; i<len; i++){
d[i] = INT_MAX;
for(int j=0; j<pre[i].size(); j++){
d[i] = min(d[i], d[pre[i][j]] + 1);
}
for(int j=i+1; j<=(nums[i] + i) && j<len; j++){
pre[j].push_back(i);
}
}
return d[len-1];
}
};```

```class Solution {
public:
int jump(vector<int>& nums) {
int len = nums.size();
if(len<2){
return 0;
}
int d[len];
for(int i=1; i<len; i++){
d[i] = INT_MAX;
}
d[0] = 0;
for(int i=0; i<len; i++){
for(int j=i+1; j<=(nums[i]+i) && j<len; j++){
d[j]=min(d[j], d[i] + 1);
}
}
return d[len-1];
}
};```

```class Solution {
public:
int jump(vector<int>& nums) {
int len = nums.size();
int left = 0;
int right = 0;
int count = 0;
while(right<len-1){
count++;
int mostRight = right;
for(int i=left; i<=right; i++){
mostRight = max(mostRight, i+nums[i]);
if(mostRight>=len - 1){
break;
}
}
left = right + 1;
right=mostRight;
if(right<left){
return -1;
}
}

return count;
}
};```