-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGameII.java
More file actions
60 lines (59 loc) · 1.78 KB
/
JumpGameII.java
File metadata and controls
60 lines (59 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package hard;
/**
* ClassName: JumpGameII.java
* Author: chenyiAlone
* Create Time: 2019/5/19 14:54
* Description: No.45
* 思路:
* 每次想最远的地方跳,更新为上一个位置的步数 + 1
* 自己想出了更好的办法,看了看跟 Discuss 上的是一样的,可是没有实现,
* 自己没能够捋出这个跳一次和上一次跳的最远距离之间的关系
*
*
*
* 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.
*
* Example:
*
* Input: [2,3,1,1,4]
* Output: 2
* Explanation: 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.
* Note:
*
* You can assume that you can always reach the last index.
*
*/
public class JumpGameII {
public int jump(int[] nums) {
int len = nums.length;
int[] f = new int[len];
for (int i = 0; i < len; i++) {
for (int j = 1; j <= nums[i]; j++) {
if (i + j < len && f[i + j] == 0)
f[i + j] = f[i] + 1;
if (f[len - 1] != 0)
return f[len - 1];
}
}
return 0;
}
public int jumpFast(int[] nums) {
int curEnd = 0;
int curFarthest = 0;
int jump = 0;
for (int i = 0; i < nums.length - 1; i++) {
curFarthest = Math.max(curFarthest, nums[i] + i);
if (curFarthest == nums.length - 1) {++jump;break;}
if (i == curEnd) {
curEnd = curFarthest;
jump++;
}
}
return jump;
}
}