-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.java
More file actions
37 lines (29 loc) · 901 Bytes
/
Copy pathJumpGame.java
File metadata and controls
37 lines (29 loc) · 901 Bytes
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
import java.util.Arrays;
/*can be done with Kadane */
public class JumpGame {
static int[] dp; // Made dp global and static
public static void main(String[] args) {
}
public boolean canJump(int[] nums) {
dp = new int[nums.length];
Arrays.fill(dp, -1);
return fxn(0, nums);
}
public boolean fxn(int i, int[] nums) {
if (i >= nums.length - 1)
return true;
if (nums[i] == 0)
return false;
if (dp[i] != -1)
return dp[i] == 1; // Adjusted for boolean return
int can_reach = i + nums[i];
for (int jump = i + 1; jump <= can_reach && jump < nums.length; jump++) { // Fixed loop condition
if (fxn(jump, nums)) {
dp[i] = 1;
return true; // True
}
}
dp[i] = 0;
return false; // False
}
}