Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 437ff63

Browse files
committedSep 16, 2021
add a solution for 55
1 parent 23e0ca9 commit 437ff63

File tree

2 files changed

+136
-7
lines changed

2 files changed

+136
-7
lines changed
 

‎src/main/java/com/fishercoder/solutions/_55.java

+71-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,80 @@
33
public class _55 {
44

55
public static class Solution1 {
6+
/**
7+
* My very original but lengthy solution.
8+
*/
69
public boolean canJump(int[] nums) {
7-
int farthest = nums[0];
8-
for (int i = 0; i < nums.length; i++) {
9-
if (i <= farthest && nums[i] + i > farthest) {
10-
//i <= farthest is to make sure that this current i is within the current range
11-
// nums[i]+i > farthest is to make sure that it's necessary to update farthest with current nums[i]+i
12-
farthest = nums[i] + i;
10+
int furthestReach = nums[0];
11+
if (furthestReach >= nums.length - 1) {
12+
return true;
13+
}
14+
int i = 1;
15+
for (; i < nums.length; ) {
16+
int newFurthestReach = -1;
17+
while (i <= furthestReach) {
18+
newFurthestReach = Math.max(newFurthestReach, nums[i] + i);
19+
if (newFurthestReach >= nums.length) {
20+
return true;
21+
}
22+
i++;
23+
}
24+
if (newFurthestReach <= furthestReach) {
25+
return false;
26+
} else if (newFurthestReach >= nums.length - 1) {
27+
return true;
28+
} else {
29+
furthestReach = newFurthestReach;
30+
}
31+
}
32+
return false;
33+
}
34+
}
35+
36+
public static class Solution2 {
37+
/**
38+
* The same idea as mine above, but much more concise.
39+
* Credit: https://leetcode.com/problems/jump-game/discuss/20917/Linear-and-simple-solution-in-C%2B%2B
40+
*/
41+
public boolean canJump(int[] nums) {
42+
int i = 0;
43+
for (int reach = 0; i < nums.length && i <= reach; i++) {
44+
reach = Math.max(reach, nums[i] + i);
45+
}
46+
return i >= nums.length;
47+
}
48+
}
49+
50+
public static class Solution3 {
51+
/**
52+
* Top-down DP.
53+
* Credit: https://leetcode.com/problems/jump-game/solution/ approach 2
54+
* <p>
55+
* Specifically, for this problem, my very own Solution1 and the above Solution2 run much faster than this DP solution.
56+
* But just use this problem to practice DP.
57+
* <p>
58+
* The reason it's called top-down is that it's filling the dp array from the right to the left if you set break points and step through this.
59+
*/
60+
public boolean canJump(int[] nums) {
61+
int[] dp = new int[nums.length];
62+
//0 means unknown, 1 means reachable, 2 means unreachable
63+
dp[nums.length - 1] = 1;
64+
return canJumpFrom(0, nums, dp);
65+
}
66+
67+
private boolean canJumpFrom(int index, int[] nums, int[] dp) {
68+
if (dp[index] != 0) {
69+
return dp[index] == 1;
70+
}
71+
int furthestReach = Math.min(index + nums[index], nums.length - 1);
72+
for (int i = index + 1; i <= furthestReach; i++) {
73+
if (canJumpFrom(i, nums, dp)) {
74+
dp[i] = 1;
75+
return true;
1376
}
1477
}
15-
return farthest >= nums.length - 1;
78+
dp[index] = 2;
79+
return false;
1680
}
1781
}
1882
}

‎src/test/java/com/fishercoder/_55Test.java

+65
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,88 @@
88

99
public class _55Test {
1010
private static _55.Solution1 solution1;
11+
private static _55.Solution2 solution2;
12+
private static _55.Solution3 solution3;
1113
private static int[] nums;
1214

1315
@BeforeClass
1416
public static void setup() {
1517
solution1 = new _55.Solution1();
18+
solution2 = new _55.Solution2();
19+
solution3 = new _55.Solution3();
1620
}
1721

1822
@Test
1923
public void test1() {
2024
nums = new int[]{0, 2, 3};
2125
assertEquals(false, solution1.canJump(nums));
26+
assertEquals(false, solution2.canJump(nums));
27+
assertEquals(false, solution3.canJump(nums));
2228
}
2329

2430
@Test
2531
public void test2() {
2632
nums = new int[]{1, 2};
2733
assertEquals(true, solution1.canJump(nums));
34+
assertEquals(true, solution2.canJump(nums));
35+
assertEquals(true, solution3.canJump(nums));
2836
}
2937

38+
@Test
39+
public void test3() {
40+
nums = new int[]{2, 3, 1, 1, 4};
41+
assertEquals(true, solution1.canJump(nums));
42+
assertEquals(true, solution2.canJump(nums));
43+
assertEquals(true, solution3.canJump(nums));
44+
}
45+
46+
@Test
47+
public void test4() {
48+
nums = new int[]{5, 9, 3, 2, 1, 0, 2, 3, 3, 1, 0, 0};
49+
assertEquals(true, solution1.canJump(nums));
50+
assertEquals(true, solution2.canJump(nums));
51+
assertEquals(true, solution3.canJump(nums));
52+
}
53+
54+
@Test
55+
public void test5() {
56+
nums = new int[]{2, 0, 0};
57+
assertEquals(true, solution1.canJump(nums));
58+
assertEquals(true, solution2.canJump(nums));
59+
assertEquals(true, solution3.canJump(nums));
60+
}
61+
62+
@Test
63+
public void test6() {
64+
nums = new int[]{2, 0};
65+
assertEquals(true, solution1.canJump(nums));
66+
assertEquals(true, solution2.canJump(nums));
67+
assertEquals(true, solution3.canJump(nums));
68+
}
69+
70+
@Test
71+
public void test7() {
72+
nums = new int[]{1, 1, 0, 1};
73+
assertEquals(false, solution1.canJump(nums));
74+
assertEquals(false, solution2.canJump(nums));
75+
assertEquals(false, solution3.canJump(nums));
76+
}
77+
78+
@Test
79+
public void test8() {
80+
nums = new int[]{0};
81+
assertEquals(true, solution1.canJump(nums));
82+
assertEquals(true, solution2.canJump(nums));
83+
assertEquals(true, solution3.canJump(nums));
84+
}
85+
86+
@Test
87+
public void test9() {
88+
nums = new int[]{3, 2, 1, 0, 4};
89+
// assertEquals(false, solution1.canJump(nums));
90+
// assertEquals(false, solution2.canJump(nums));
91+
assertEquals(false, solution3.canJump(nums));
92+
}
93+
94+
3095
}

0 commit comments

Comments
 (0)
Failed to load comments.