Jump Game
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.
Determine if you are able to reach the last index.
For example: A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Ideas: maximum jump, so it means can jump between 0~A[i]
Greedy to get max for each i.
Code:
public boolean canJump(int[] A) {
int len = A.length;
if (len <= 1) return true;
int max = A[0];
for (int i = 0; i < len -1; i++) {
if (max >= i && i + A[i] >= len - 1) {
return true;
}
if (max < i || max == i && A[i] == 0) {
return false;
}
if (max < A[i] + i) {
max = A[i] + i;
}
}
return false;
}