-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpGame2.java
50 lines (41 loc) · 1.13 KB
/
JumpGame2.java
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
// GreedyAlgorithm
// my solution_O(n)
public class JumpGame2 {
public int jump(int[] nums) {
int maxStep = 0;
int cnt = 0;
int minStep = 0;
if (nums.length == 0 || nums.length ==1)
return 0;
for (int i = 0; i < nums.length; i++) {
// 최대 접근 가능 위치 삽입
maxStep = Math.max(maxStep, i + nums[i]);
if(i == minStep) {
if(i == nums.length-1)
break;
minStep = maxStep;
cnt++; //이동
}
}
return cnt;
}
}
/* DP
* Another Solution
class Solution {
public int jump(int[] nums) {
int[] cache = new int[nums.length];
cache[0] = 0;
for (int i = 1; i < cache.length; i++) {
cache[i] = Integer.MAX_VALUE;
}
for (int i = 0; i < nums.length; i++) {
for (int j = 1; j <= nums[i]; j++) {
int next = Math.min(i+j, nums.length-1);
cache[next] = Math.min(cache[next], 1 + cache[i]);
}
}
return cache[nums.length-1];
}
}
*/