Skip to content

Commit

Permalink
https://leetcode.com/problems/jump-game/
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Oct 29, 2023
1 parent 3209d58 commit 09b8242
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package hoa.can.code.ez;

import java.util.Arrays;

import util.Array;

public class PartitionEqualSubSetSum {
Expand All @@ -12,7 +10,6 @@ public static int equalPartition(int n, int arr[]){
}

static int canAddUp(int arr[], int target){
System.out.println(String.format("canAddUp %d %s", target, Arrays.toString(arr)));
if(target==0){
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import java.util.Arrays;

/**
* https://leetcode.com/problems/jump-game/
* <a href="https://leetcode.com/problems/jump-game/">Desc</a>
*/
public class Jumping {
final static int NO_WAY = Integer.MAX_VALUE;
final static int NO_WAY = 10000;

public static boolean canJump(int[] nums) {
int[] memo = new int[nums.length];
Arrays.fill(memo, -1);
return (minJumps(nums, 0, nums.length - 1, memo) > -1);
int step = minJumps(nums, 0, nums.length - 1, memo);
//if (nums.length == 1 && step == 0) return false;
return (step != NO_WAY);
}
private static int minJumps(int steps[], int begin, int dest, int[] memo) {

private static int minJumps(int[] steps, int begin, int dest, int[] memo) {
if (begin == dest)
return 0;
if (steps[begin] == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@

import static hoa.can.code.gg.Jumping.canJump;
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}));
}

@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}));
}
}

0 comments on commit 09b8242

Please sign in to comment.