题目描述
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, then3steps to the last index.)
分析:相比之前的jump-game,多了一步用step记录每次必须跳跃的记录。循环终止的条件是最大距离位置无法到达当前位置。
class Solution {
public:
// 当前能够到达的最远距离还没有到n,必须再走下一步
int jump(int A[], int n)
{
int curReach=0,maxReach = 0,steps=0;
for(int i=0;i<n && i<=maxReach;i++)
{
if(i>curReach) //steps-1步能够到达的距离,必须再走一步了
{
++steps;
curReach = maxReach;
}
maxReach = max(maxReach,i+A[i]); // step步最远距离
}
if(maxReach<n-1)
return -1;
else
return steps;
}
};