-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://leetcode.com/problems/jump-game/
- Loading branch information
Showing
6 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/JumpingDP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hoa.can.code.gg; | ||
|
||
public class JumpingDP { | ||
public static boolean canJump(int[] nums) { | ||
return minJumps(nums) != Integer.MAX_VALUE; | ||
} | ||
|
||
public static int minJumps(int[] arr) { | ||
int n = arr.length; | ||
int minCostToIdx[] = new int[n]; | ||
// result | ||
int i, j; | ||
|
||
// if first element is 0, | ||
if (n == 0 || arr[0] == 0) | ||
return Integer.MAX_VALUE; | ||
|
||
minCostToIdx[0] = 0; | ||
|
||
for (i = 1; i < n; i++) { | ||
minCostToIdx[i] = Integer.MAX_VALUE; | ||
for (j = 0; j < i; j++) { | ||
if (i <= j + arr[j] && minCostToIdx[j] != Integer.MAX_VALUE) { | ||
minCostToIdx[i] = Math.min(minCostToIdx[i], minCostToIdx[j] + 1); | ||
break; | ||
} | ||
} | ||
} | ||
return minCostToIdx[n - 1]; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/JumpingGreedily.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package hoa.can.code.gg; | ||
|
||
public class JumpingGreedily { | ||
final static int NO_WAY = 10000; | ||
|
||
public static boolean canJump(int[] nums) { | ||
return minJumps(nums) != NO_WAY; | ||
} | ||
|
||
public static int minJumps(int[] nums) { | ||
int n = nums.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
int maxReach = nums[0]; | ||
int currentMax = nums[0]; | ||
|
||
if (currentMax == 0) return NO_WAY; | ||
if (currentMax >= n - 1) return 1; | ||
|
||
int steps = 1; | ||
|
||
for (int currentBase = 1; currentBase < n; currentBase++) { | ||
if (currentBase > maxReach) { | ||
steps++; | ||
maxReach = currentMax; | ||
if (currentMax >= n - 1) return steps; | ||
} | ||
if (nums[currentBase] == 0) { | ||
if ((currentMax == currentBase)) { | ||
return NO_WAY; | ||
} | ||
if (currentMax >= n - 1) { | ||
steps++; | ||
return steps; | ||
} | ||
} | ||
|
||
currentMax = Math.max(currentMax, currentBase + nums[currentBase]); | ||
} | ||
return steps; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/JumpingRecursive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hoa.can.code.gg; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* <a href="https://leetcode.com/problems/jump-game/">Desc</a> | ||
*/ | ||
public class JumpingRecursive { | ||
final static int NO_WAY = 10000; | ||
|
||
public static boolean canJump(int[] nums) { | ||
int step = minJumps(nums); | ||
return (step != NO_WAY); | ||
} | ||
|
||
public static int minJumps(int[] nums) { | ||
int[] memo = new int[nums.length]; | ||
Arrays.fill(memo, -1); | ||
int lastIdx = nums.length - 1; | ||
return minJumps(nums, 0, lastIdx, memo); | ||
} | ||
|
||
private static int minJumps(int[] steps, int begin, int dest, int[] memo) { | ||
if (begin == dest) | ||
return 0; | ||
if (steps[begin] == 0) | ||
return NO_WAY; | ||
|
||
int minJump = NO_WAY; | ||
for (int step = steps[begin]; step >= 1; --step) { | ||
if (begin + step <= dest) { | ||
minJump = Math.min(minJump, 1 + minJumps(steps, begin + step, dest, memo)); | ||
} | ||
} | ||
return memo[begin] = minJump; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
interview_prep/algorithm/java/src/test/java/hoa/can/code/JumpingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package hoa.can.code; | ||
|
||
import hoa.can.code.gg.JumpingDP; | ||
import hoa.can.code.gg.JumpingGreedily; | ||
import hoa.can.code.gg.JumpingRecursive; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static java.lang.String.format; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class JumpingTest { | ||
@Test | ||
@DisplayName("ok") | ||
public void yes() { | ||
//canJump(new int[]{0});// depends | ||
canJump(new int[]{2, 3, 1, 1, 4}); | ||
canJump(new int[]{1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}); | ||
canJump(new int[]{3, 2, 1, 1, 1, 2, 3, 4, 5, 1, 3, 0, 2, 3, 0, 3, 1, 2, 3, 9, 1, 0, 0, 4, 1, 1, 1, 0, 2, 3, 4, 0, 9, 0, 0, 1, 2, 3, 9, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4}); | ||
} | ||
|
||
@Test | ||
@DisplayName("nope") | ||
public void no() { | ||
canNotJump(new int[]{0, 1}); | ||
canNotJump(new int[]{3, 2, 1, 0, 4}); | ||
canNotJump(new int[]{2, 0, 0, 0, 3}); | ||
canNotJump(new int[]{4, 3, 2, 1, 0, 0, 0}); | ||
canNotJump(new int[]{3, 2, 1, 0, 1, 2, 3, 4, 5, 1, 0, 0, 2, 3, 0, 0, 1, 2, 3, 0, 1, 0, 0, 4, 1, 1, 1, 0, 2, 3, 4, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4}); | ||
} | ||
|
||
@Test | ||
@DisplayName("yes-WithDetail") | ||
public void yesWithDetail() { | ||
yesWithDetail(new int[]{1, 2}, 1, "1 -> end"); | ||
yesWithDetail(new int[]{2,0,1},1, "2 -> end"); | ||
yesWithDetail(new int[]{2, 3, 1, 1, 4}, 2, "2 -> 3 -> end"); | ||
yesWithDetail(new int[]{10, 8, 12, 17, 1, 21, 5, 17, 20, 11, 5, 27, 23, 8, 12, 18, 16, 12, 9, 8, 17, 12, 23, 26, 0, 5, 9, 24, 10}, 2, "10 -> 20 -> end"); | ||
yesWithDetail(new int[]{5, 9, 3, 2, 1, 0, 2, 3, 3, 1, 0, 0}, 3, "5->9->3->end"); | ||
yesWithDetail(new int[]{1, 1, 1, 1, 1, 1}, 5, "1 -> 1 ->... end"); | ||
} | ||
|
||
@Test | ||
@DisplayName("nope") | ||
@Disabled("TOO SLOW TO CHECK ALL COMBINATIONS") | ||
public void nope() { | ||
assertEquals( | ||
13, | ||
JumpingRecursive.minJumps(new int[]{8, 2, 4, 4, 4, 9, 5, 2, 5, 8, 8, 0, 8, 6, 9, 1, 1, 6, 3, 5, 1, 2, 6, 6, 0, 4, 8, 6, 0, 3, 2, 8, 7, 6, 5, 1, 7, 0, 3, 4, 8, 3, 5, 9, 0, 4, 0, 1, 0, 5, 9, 2, 0, 7, 0, 2, 1, 0, 8, 2, 5, 1, 2, 3, 9, 7, 4, 7, 0, 0, 1, 8, 5, 6, 7, 5, 1, 9, 9, 3, 5, 0, 7, 5}), | ||
"quick" | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("yep") | ||
public void yep() { | ||
assertEquals( | ||
13, | ||
JumpingGreedily.minJumps(new int[]{8,2,4,4,4,9,5,2,5,8,8,0,8,6,9,1,1,6,3,5,1,2,6,6,0,4,8,6,0,3,2,8,7,6,5,1,7,0,3,4,8,3,5,9,0,4,0,1,0,5,9,2,0,7,0,2,1,0,8,2,5,1,2,3,9,7,4,7,0,0,1,8,5,6,7,5,1,9,9,3,5,0,7,5}), | ||
"quick" | ||
); | ||
assertEquals( | ||
13, | ||
JumpingDP.minJumps(new int[]{8, 2, 4, 4, 4, 9, 5, 2, 5, 8, 8, 0, 8, 6, 9, 1, 1, 6, 3, 5, 1, 2, 6, 6, 0, 4, 8, 6, 0, 3, 2, 8, 7, 6, 5, 1, 7, 0, 3, 4, 8, 3, 5, 9, 0, 4, 0, 1, 0, 5, 9, 2, 0, 7, 0, 2, 1, 0, 8, 2, 5, 1, 2, 3, 9, 7, 4, 7, 0, 0, 1, 8, 5, 6, 7, 5, 1, 9, 9, 3, 5, 0, 7, 5}), | ||
"quick" | ||
); | ||
} | ||
|
||
void canJump(int[] steps) { | ||
String log = format("%s Should be REACHABLE", Arrays.toString(steps)); | ||
assertTrue(JumpingRecursive.canJump(steps), log); | ||
assertTrue(JumpingGreedily.canJump(steps), log); | ||
assertTrue(JumpingDP.canJump(steps), log); | ||
} | ||
|
||
void canNotJump(int[] steps) { | ||
assertFalse(JumpingRecursive.canJump(steps)); | ||
assertFalse(JumpingGreedily.canJump(steps)); | ||
assertFalse(JumpingDP.canJump(steps)); | ||
} | ||
|
||
void yesWithDetail(int[] steps, int expected, String msg) { | ||
String log = format("\n%s \n%s", Arrays.toString(steps), msg); | ||
assertEquals(expected, JumpingRecursive.minJumps(steps), log); | ||
assertEquals(expected, JumpingGreedily.minJumps(steps), log); | ||
assertEquals(expected, JumpingDP.minJumps(steps), log); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../hoa/can/PartitionEqualSubSetSumTest.java → ...can/code/PartitionEqualSubSetSumTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package hoa.can; | ||
package hoa.can.code; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
|