Given an array of non-negative integers, you is initially positioned at the first index of the array.
Each element of the array represents your maximum jump length is at that position.
Determine if you is able to reach the last index.
For example:
A = [2,3,1,1,4] , return true .
A = [3,2,1,0,4] , return false .
Idea one: The greedy method determines the maximum distance each point can reach and, within the farthest distance that can be reached, continues to iterate, calculates the farthest distance, and finally determines whether the Maxreach reaches n-1 place.
I <= Maxreach This judging condition is very critical
classSolution { Public: BOOLCanjump (intA[],intN) {intMaxreach =0;//The most rigth we can reach for(inti =0;I <= Maxreach&& i < n; i++) { //cout << maxreach << Endl;Maxreach = max (Maxreach, i +A[i]); } returnMaxreach >= N-1; } };
[Leetcode] Jump Game