diff --git a/interview_prep/algorithm/java/src/main/java/hoa/can/code/ez/PartitionEqualSubSetSum.java b/interview_prep/algorithm/java/src/main/java/hoa/can/code/ez/PartitionEqualSubSetSum.java index 068ba5fd..611c321c 100644 --- a/interview_prep/algorithm/java/src/main/java/hoa/can/code/ez/PartitionEqualSubSetSum.java +++ b/interview_prep/algorithm/java/src/main/java/hoa/can/code/ez/PartitionEqualSubSetSum.java @@ -1,7 +1,5 @@ package hoa.can.code.ez; -import java.util.Arrays; - import util.Array; public class PartitionEqualSubSetSum { @@ -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; } diff --git a/interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/Jumping.java b/interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/Jumping.java index 340090f2..1ecb9988 100644 --- a/interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/Jumping.java +++ b/interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/Jumping.java @@ -3,16 +3,25 @@ import java.util.Arrays; /** - * https://leetcode.com/problems/jump-game/ + * Desc */ public class Jumping { - final static int NO_WAY = Integer.MAX_VALUE; + 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); - return (minJumps(nums, 0, nums.length - 1, memo) > -1); + int lastIdx = nums.length - 1; + int r = minJumps(nums, 0, lastIdx, memo); + return r; } - 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) diff --git a/interview_prep/algorithm/java/src/test/java/hoa/can/code/JumpingTest.java b/interview_prep/algorithm/java/src/test/java/hoa/can/code/JumpingTest.java index ff7c578d..6667cb3e 100644 --- a/interview_prep/algorithm/java/src/test/java/hoa/can/code/JumpingTest.java +++ b/interview_prep/algorithm/java/src/test/java/hoa/can/code/JumpingTest.java @@ -4,20 +4,56 @@ 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("10 -> 20 -> end") + 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" + ); } } diff --git a/interview_prep/algorithm/java/src/test/java/hoa/can/PartitionEqualSubSetSumTest.java b/interview_prep/algorithm/java/src/test/java/hoa/can/code/PartitionEqualSubSetSumTest.java similarity index 96% rename from interview_prep/algorithm/java/src/test/java/hoa/can/PartitionEqualSubSetSumTest.java rename to interview_prep/algorithm/java/src/test/java/hoa/can/code/PartitionEqualSubSetSumTest.java index de629b97..550a98a5 100644 --- a/interview_prep/algorithm/java/src/test/java/hoa/can/PartitionEqualSubSetSumTest.java +++ b/interview_prep/algorithm/java/src/test/java/hoa/can/code/PartitionEqualSubSetSumTest.java @@ -1,4 +1,4 @@ -package hoa.can; +package hoa.can.code; import static org.junit.jupiter.api.Assertions.assertEquals;