-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJumpGame.java
More file actions
44 lines (41 loc) · 1.53 KB
/
JumpGame.java
File metadata and controls
44 lines (41 loc) · 1.53 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
import java.io.*;
import java.util.Properties;
class JumpGame {
public boolean canJump(int[] nums) {
if (nums == null || nums.length == 0)
return false;
if (nums.length == 1)
return true;
boolean[] cache = new boolean [nums.length];
return canJump(nums, 0, cache);
}
boolean canJump(int[] nums, int currentPosition, boolean[] cache) {
if (currentPosition == nums.length - 1) {
cache[currentPosition] = true;
return cache[currentPosition];
}
if (currentPosition >= nums.length)
return false;
if (cache[currentPosition])
return true;
for (int i = nums[currentPosition]; i > 0; i--) {
System.out.println(currentPosition);
boolean isPossible = canJump(nums, currentPosition + i, cache);
if (isPossible) {
System.out.println(currentPosition);
cache[currentPosition] = true;
return cache[currentPosition];
}
}
cache[currentPosition] = false;
return cache[currentPosition];
}
public static void main(String[] args) throws IOException {
JumpGame jumpGame = new JumpGame();
Properties properties = new Properties();
InputStream inputStream = jumpGame.getClass().getResourceAsStream("jumpGame.properties");
properties.load(inputStream);
int[] nums = {2, 1, 1, 4, 5, 2, 1, 0, 0, 0};
System.out.println(jumpGame.canJump(nums));
}
}