-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathJumpGame.java
66 lines (58 loc) · 1.83 KB
/
JumpGame.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package problems.medium;
import java.util.*;
/**
* Created by sherxon on 2/13/17.
*/
/**
* 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.
*/
public class JumpGame {
public static void main(String[] args) {
System.out.println(canJump(new int[]{
//3,2,1,0,4
//2,3,1,1,4
2, 0
//1,0
}));
}
static boolean canJump(int[] a) {
if (a.length == 0) return true;
if (a.length == 1) return true;
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (max < i) return false;
if (max >= a.length - 1) return true;
max = Math.max(i + a[i], max);
}
return max >= a.length - 1;
}
public boolean canJump2(int[] a) {
if (a.length == 0) return true;
if (a.length == 1) return true;
Map<Integer, Set<Integer>> map = new HashMap<>(a.length);
for (int i = 0; i < a.length; i++) {
if (!map.containsKey(i)) {
map.put(i, new HashSet<>());
}
for (int j = i + 1; j < a[i] + i + 1; j++) {
map.get(i).add(j);
}
}
boolean[] visited = new boolean[a.length];
Queue<Integer> q = new LinkedList<>();
q.add(0);
while (!q.isEmpty()) {
Integer x = q.remove();
for (Integer integer : map.get(x)) {
if (integer == a.length - 1) return true;
if (!visited[integer]) {
visited[integer] = true;
q.add(integer);
}
}
}
return false;
}
}