From c6e01a8d93e8971a672348cbd9b2bab0d174fd95 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sat, 19 Oct 2024 21:39:14 +0530 Subject: [PATCH 1/6] Add edge case to handle negative rod length in RodCutting algorithm --- .../com/thealgorithms/dynamicprogramming/RodCutting.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 76b341e2c823..57101a85b070 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -22,6 +22,9 @@ public static int cutRod(int[] price, int n) { if (price == null || price.length == 0) { throw new IllegalArgumentException("Price array cannot be null or empty."); } + if (n < 0) { + throw new IllegalArgumentException("Rod length cannot be negative."); + } // Create an array to store the maximum obtainable values for each rod length. int[] val = new int[n + 1]; val[0] = 0; @@ -40,4 +43,4 @@ public static int cutRod(int[] price, int n) { // The final element of 'val' contains the maximum obtainable value for a rod of length 'n'. return val[n]; } -} +} \ No newline at end of file From fd2f306a5a1eb8b7361e6c6aee617a04d0ccabc2 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sat, 19 Oct 2024 22:02:27 +0530 Subject: [PATCH 2/6] Add test case for negative rod length in RodCuttingTest --- .../thealgorithms/dynamicprogramming/RodCuttingTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 39497a768397..2d65658beccc 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -93,4 +93,11 @@ void testCutRodEmptyPrices() { int length = 5; assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException."); } + @Test + void testCutRodNegativeLength() { + int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for negative length + int length = -1; + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), + "A negative rod length should throw an IllegalArgumentException."); + } } From 77eedbe0e511af9aaa956c44120ba8bdf142b11e Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sat, 19 Oct 2024 22:19:00 +0530 Subject: [PATCH 3/6] Fix: add newline at the end of RodCutting.java --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 57101a85b070..503fab543d8a 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -25,6 +25,7 @@ public static int cutRod(int[] price, int n) { if (n < 0) { throw new IllegalArgumentException("Rod length cannot be negative."); } + // Create an array to store the maximum obtainable values for each rod length. int[] val = new int[n + 1]; val[0] = 0; @@ -43,4 +44,5 @@ public static int cutRod(int[] price, int n) { // The final element of 'val' contains the maximum obtainable value for a rod of length 'n'. return val[n]; } -} \ No newline at end of file +} + From b7368818a910a5c3fbf55bc246f27aa863a7a6e4 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sat, 19 Oct 2024 22:49:21 +0530 Subject: [PATCH 4/6] updated --- .../com/thealgorithms/dynamicprogramming/RodCuttingTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 2d65658beccc..7f3bb7eba6bf 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -101,3 +101,4 @@ void testCutRodNegativeLength() { "A negative rod length should throw an IllegalArgumentException."); } } + From e145f543532ef418fd4fa8ef8ba21ba882b1bf2c Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sat, 19 Oct 2024 23:05:10 +0530 Subject: [PATCH 5/6] Fix issues from pull request review --- .../com/thealgorithms/dynamicprogramming/RodCuttingTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 7f3bb7eba6bf..9cf21fd836db 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -97,8 +97,6 @@ void testCutRodEmptyPrices() { void testCutRodNegativeLength() { int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for negative length int length = -1; - assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), - "A negative rod length should throw an IllegalArgumentException."); + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "A negative rod length should throw an IllegalArgumentException."); } } - From 1241879eb8fae5b12ba2ce9c5cb51de26cbbc43d Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sat, 19 Oct 2024 23:06:36 +0530 Subject: [PATCH 6/6] Updated checks in RodCutting.java --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 503fab543d8a..6d33826b6b17 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -45,4 +45,3 @@ public static int cutRod(int[] price, int n) { return val[n]; } } -