【题目描述】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 is2. (Jump1step from index 0 to 1, then 3 steps to the last index.)
【解题思路】倒推
class Solution {
public:
int jump(int A[], int n) {
int pre = 0;
int cur = n - 1;
int count = 0;
while(true){
if(pre == cur)
return 0;
count++;
pre = cur;
for(int i = n - 2; i >= 0; i--){
if(i + A[i] >= pre)
if(cur > i)
cur = i;
}
if(cur == 0)
return count;
}
}
};