I declare two para ; boundary start and boundary end and for each step I update the new boundary start and the boundary end.
if the boundary end >= n-1 we get the signal that we can reach the tail of the whole array.
However, you must pay attention to a situation, that in this step, we cannot get a further tail.
eg.
5 9 3 2 1 0 1 1 1 1 0 0
so that we still cannot reach to the tail.
so we must judge if in this step, we cannot have a further tail, we must return 0;
class Solution {
public:
int jump(int A[], int n){
vector <int> temp;
int step = 0;
if(n == 0 || n == 1)
return step;
int size = 0;;
int i = 0;
int boundrystart = 0;
int boundryend = A[0];
size = boundryend-boundrystart;
while(boundrystart <= boundryend)
{
step ++;
if(boundryend >=n-1 )
return step;
for(int i =boundrystart+1; i<= size+boundrystart;i++)
{
if(i+A[i]>boundryend)
boundryend = A[i]+i;
}
if(boundryend == boundrystart + size)
return 0;
boundrystart = boundrystart + size;
size = boundryend - boundrystart;
}
return 0;
}
};