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.
收敛思维:i 能且只能在当前可达范围前移。
循环不变式:在当前可达范围内往前移,同时维护reach保证其为已知可达最远点。
bool canJump(int A[], int n) {
for (int i = 0, reach = 0; i <= reach; ++i) {
reach = max(reach, i + A[i]);
if (reach >= n - 1) return true;
}
return false;
}