-
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
5 changed files
with
89 additions
and
12 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
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
24 changes: 24 additions & 0 deletions
24
interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/JumpingQuickly.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,24 @@ | ||
package hoa.can.code.gg; | ||
|
||
public class JumpingQuickly { | ||
public static int minJumps(int[] nums) { | ||
int n = nums.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
int maxReach = nums[0]; | ||
int steps = 1; | ||
int currentMax = nums[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
if (i > maxReach) { | ||
steps++; | ||
maxReach = currentMax; | ||
} | ||
|
||
currentMax = Math.max(currentMax, i + nums[i]); | ||
} | ||
return steps; | ||
} | ||
} |
56 changes: 52 additions & 4 deletions
56
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 |
---|---|---|
@@ -1,23 +1,71 @@ | ||
package hoa.can.code; | ||
|
||
import hoa.can.code.gg.JumpingQuickly; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static hoa.can.code.gg.Jumping.canJump; | ||
import static hoa.can.code.gg.Jumping.minJumps; | ||
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() { | ||
assertEquals(true, canJump(new int[]{2, 3, 1, 1, 4})); | ||
assertEquals(true, canJump(new int[]{1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9})); | ||
assertTrue(canJump(new int[]{0})); | ||
assertTrue(canJump(new int[]{2, 3, 1, 1, 4})); | ||
assertTrue(canJump(new int[]{1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9})); | ||
assertTrue(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() { | ||
assertEquals(true, canJump(new int[]{0})); | ||
assertEquals(true, canJump(new int[]{0,1})); | ||
assertFalse(canJump(new int[]{0, 1})); | ||
assertFalse(canJump(new int[]{3, 2, 1, 0, 4})); | ||
assertFalse(canJump(new int[]{2, 0, 0, 0, 3})); | ||
assertFalse(canJump(new int[]{4, 3, 2, 1, 0, 0, 0})); | ||
assertFalse(canJump(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() { | ||
assertEquals( | ||
2, | ||
minJumps(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}), | ||
"10 -> 20 -> end" | ||
); | ||
|
||
assertEquals( | ||
2, | ||
minJumps(new int[]{2,3,1,1,4}), | ||
"2 -> 3 -> end" | ||
); | ||
|
||
assertEquals( | ||
3, | ||
minJumps(new int[]{2,3,1,1,1,4}), | ||
"2 -> 3 -> end" | ||
); | ||
|
||
assertEquals( | ||
5, | ||
minJumps(new int[]{1,1,1,1,1,1}), | ||
"2 -> 3 -> end" | ||
); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("nope") | ||
public void nope() { | ||
assertEquals( | ||
13, | ||
JumpingQuickly.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" | ||
); | ||
} | ||
} |
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; | ||
|
||
|