/*
we start from the end if the element at i is greater than or equal
to the distance between the last element and it's position
then we can jump from that position to the end of the array.
we reset n to the current position and start the search again.
if we reach 1st index, that means we can complete the traversal
else we can't
*/
int Solution::canJump(vector<int> &A)
{
int n = A.size();
for(int i = A.size()-1; i>=0; i--)
{
if(A[i] >= n-i-1)
n = i+1;
}
return n == 1;
}
Very readable easy to understand code in O(n) with intuition and working commented | C++
porygon-Z
#1