-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.java
More file actions
48 lines (36 loc) · 1.17 KB
/
Copy pathJumpGame.java
File metadata and controls
48 lines (36 loc) · 1.17 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
class Solution {
public boolean canJump(int[] nums) {
if (nums.length == 1)
return true;
boolean[] reachableFields = new boolean[nums.length];
reachableFields[0] = true;
int i = 0;
int j = 1;
while (i < nums.length && j < nums.length) {
while (i < nums.length && reachableFields[i] == false) {
i++;
}
if (i == nums.length || j == nums.length) {
break;
}
for (int i2 = i+1; i2 < nums.length && i2 <= (i+nums[i]); i2++) {
reachableFields[i2] = true;
}
i++;
if (i == nums.length || j == nums.length) {
break;
}
while (j < nums.length && reachableFields[j] == false) {
j++;
}
if (i == nums.length || j == nums.length) {
break;
}
for (int j2 = j+1; j2 <= (j+nums[j]) && j2 < nums.length; j2++) {
reachableFields[j2] = true;
}
j++;
}
return reachableFields[reachableFields.length-1];
}
}