1.1 题目

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.

1.2 解题思路

1. 能跳到位置i的条件：i<=maxIndex。
2. 一旦跳到i，则maxIndex = max(maxIndex, i+A[i])。
3. 能跳到最后一个位置n-1的条件是：maxIndex >= n-1

1.3 解题代码

``````public class Solution {
public boolean canJump(int[] A) {
// think it as merging n intervals
if (A == null || A.length == 0) {
return false;
}
int farthest = A[0];
for (int i = 1; i < A.length; i++) {
if (i <= farthest && A[i] + i >= farthest) {
farthest = A[i] + i;
}
}
return farthest >= A.length - 1;
}
}``````

2.1 题目

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.

Your goal is to reach the last index in the minimum number of jumps.

For example: Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

2.2 解题思路

d[k] = max(i+A[i]) d[k-2] < i <= d[k-1]

2.3 解题代码

``````public class Solution {
public int jump(int[] A) {
if (A == null || A.length == 0) {
return -1;
}
int start = 0, end = 0, jumps = 0;
while (end < A.length - 1) {
jumps++;
int farthest = end;
for (int i = start; i <= end; i++) {
if (A[i] + i > farthest) {
farthest = A[i] + i;
}
}
start = end + 1;
end = farthest;
}
return jumps;
}
}``````