From 0c759569d28915119f7049559a91d427b0218d6e Mon Sep 17 00:00:00 2001 From: Manish Raj <2200032955@kluniversity.in> Date: Mon, 28 Oct 2024 13:36:48 +0530 Subject: [PATCH 1/3] Update EggDropping.java ## Changes Made 1. Improved Documentation - Added comprehensive class-level JavaDoc - Added detailed method documentation with @param and @return tags - Added time and space complexity analysis (O(n*m*m) time, O(n*m) space) - Added comments explaining DP state transitions and base cases 2. Code Quality Improvements - Made class final and constructor private (utility class best practice) - Added try-catch block in main method for better error handling The core dynamic programming algorithm remains unchanged, maintaining its educational value while improving code quality and documentation. --- .../dynamicprogramming/EggDropping.java | 95 ++++++++++++------- 1 file changed, 61 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java index be52ab166f18..179a4b226162 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java @@ -1,51 +1,78 @@ package com.thealgorithms.dynamicprogramming; /** - * DynamicProgramming solution for the Egg Dropping Puzzle + * Dynamic Programming solution for the Egg Dropping Puzzle + * The problem is to find the minimum number of attempts needed in the worst case to find the critical + * floor from which if an egg is dropped, it will break. + * Time Complexity: O(n * m * m), where n is number of eggs and m is number of floors + * Space Complexity: O(n * m) to store the DP table */ public final class EggDropping { + private EggDropping() { + // private constructor to prevent instantiation } - - // min trials with n eggs and m floors - public static int minTrials(int n, int m) { - int[][] eggFloor = new int[n + 1][m + 1]; - int result; - int x; - - for (int i = 1; i <= n; i++) { - eggFloor[i][0] = 0; // Zero trial for zero floor. - eggFloor[i][1] = 1; // One trial for one floor + + /** + * Finds minimum number of trials needed in worst case for n eggs and m floors + * + * @param eggs The number of eggs available + * @param floors The number of floors in the building + * @return Minimum number of trials needed in worst case + * @throws IllegalArgumentException if eggs <= 0 or floors < 0 + */ + public static int minTrials(int eggs, int floors) { + if (eggs <= 0 || floors < 0) { + throw new IllegalArgumentException("Number of eggs must be positive and floors must be non-negative"); } - // j trials for only 1 egg - for (int j = 1; j <= m; j++) { - eggFloor[1][j] = j; + // dp[i][j] represents minimum number of trials needed for i eggs and j floors + int[][] dp = new int[eggs + 1][floors + 1]; + + // Base case 1: Zero trials for zero floor + // Base case 2: One trial for one floor + for (int i = 1; i <= eggs; i++) { + dp[i][0] = 0; + dp[i][1] = 1; } - - // Using bottom-up approach in DP - for (int i = 2; i <= n; i++) { - for (int j = 2; j <= m; j++) { - eggFloor[i][j] = Integer.MAX_VALUE; - for (x = 1; x <= j; x++) { - result = 1 + Math.max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]); - - // choose min of all values for particular x - if (result < eggFloor[i][j]) { - eggFloor[i][j] = result; - } + + // Base case 3: With one egg, need to try every floor from bottom + for (int j = 1; j <= floors; j++) { + dp[1][j] = j; + } + + // Fill rest of the entries in table using optimal substructure property + for (int i = 2; i <= eggs; i++) { + for (int j = 2; j <= floors; j++) { + dp[i][j] = Integer.MAX_VALUE; + // Try dropping egg from each floor and find minimum trials needed + for (int k = 1; k <= j; k++) { + // Maximum of: + // 1) Egg breaks at floor k: Check below floors with i-1 eggs + // 2) Egg doesn't break: Check above floors with i eggs + int attempts = 1 + Math.max(dp[i - 1][k - 1], dp[i][j - k]); + dp[i][j] = Math.min(dp[i][j], attempts); } } } - - return eggFloor[n][m]; + + return dp[eggs][floors]; } - + + /** + * Example usage + */ public static void main(String[] args) { - int n = 2; - int m = 4; - // result outputs min no. of trials in worst case for n eggs and m floors - int result = minTrials(n, m); - System.out.println(result); + try { + // Example: 2 eggs and 4 floors + System.out.println("Minimum number of trials in worst case with 2 eggs and 4 floors: " + + minTrials(2, 4)); + + // Additional test case + System.out.println("Minimum number of trials in worst case with 3 eggs and 5 floors: " + + minTrials(3, 5)); + } catch (IllegalArgumentException e) { + System.err.println("Error: " + e.getMessage()); + } } } From 27deddcca051e776cd27cfec60f72733230f20a2 Mon Sep 17 00:00:00 2001 From: Manish Raj <2200032955@kluniversity.in> Date: Mon, 28 Oct 2024 08:19:23 +0000 Subject: [PATCH 2/3] Refactor EggDropping.java to improve readability and performance --- .../dynamicprogramming/EggDropping.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java index 179a4b226162..aca805749877 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java @@ -6,13 +6,14 @@ * floor from which if an egg is dropped, it will break. * Time Complexity: O(n * m * m), where n is number of eggs and m is number of floors * Space Complexity: O(n * m) to store the DP table + * co-author @manishraj27 */ public final class EggDropping { - + private EggDropping() { // private constructor to prevent instantiation } - + /** * Finds minimum number of trials needed in worst case for n eggs and m floors * @@ -28,19 +29,19 @@ public static int minTrials(int eggs, int floors) { // dp[i][j] represents minimum number of trials needed for i eggs and j floors int[][] dp = new int[eggs + 1][floors + 1]; - + // Base case 1: Zero trials for zero floor // Base case 2: One trial for one floor for (int i = 1; i <= eggs; i++) { dp[i][0] = 0; dp[i][1] = 1; } - + // Base case 3: With one egg, need to try every floor from bottom for (int j = 1; j <= floors; j++) { dp[1][j] = j; } - + // Fill rest of the entries in table using optimal substructure property for (int i = 2; i <= eggs; i++) { for (int j = 2; j <= floors; j++) { @@ -55,22 +56,20 @@ public static int minTrials(int eggs, int floors) { } } } - + return dp[eggs][floors]; } - + /** * Example usage */ public static void main(String[] args) { try { // Example: 2 eggs and 4 floors - System.out.println("Minimum number of trials in worst case with 2 eggs and 4 floors: " - + minTrials(2, 4)); - + System.out.println("Minimum number of trials in worst case with 2 eggs and 4 floors: " + minTrials(2, 4)); + // Additional test case - System.out.println("Minimum number of trials in worst case with 3 eggs and 5 floors: " - + minTrials(3, 5)); + System.out.println("Minimum number of trials in worst case with 3 eggs and 5 floors: " + minTrials(3, 5)); } catch (IllegalArgumentException e) { System.err.println("Error: " + e.getMessage()); } From a469160d74c652ba0c78aeb19f40fb4febb72613 Mon Sep 17 00:00:00 2001 From: Manish Raj <2200032955@kluniversity.in> Date: Tue, 29 Oct 2024 18:20:40 +0000 Subject: [PATCH 3/3] Refactor EggDropping.java to improve readability and performance --- .../dynamicprogramming/EggDropping.java | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java index aca805749877..e3012714a42a 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java @@ -1,5 +1,10 @@ package com.thealgorithms.dynamicprogramming; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + /** * Dynamic Programming solution for the Egg Dropping Puzzle * The problem is to find the minimum number of attempts needed in the worst case to find the critical @@ -60,18 +65,37 @@ public static int minTrials(int eggs, int floors) { return dp[eggs][floors]; } - /** - * Example usage - */ - public static void main(String[] args) { - try { - // Example: 2 eggs and 4 floors - System.out.println("Minimum number of trials in worst case with 2 eggs and 4 floors: " + minTrials(2, 4)); - - // Additional test case - System.out.println("Minimum number of trials in worst case with 3 eggs and 5 floors: " + minTrials(3, 5)); - } catch (IllegalArgumentException e) { - System.err.println("Error: " + e.getMessage()); - } + @Test + void testBasicScenarios() { + // Test with 2 eggs and 4 floors + assertEquals(3, minTrials(2, 4)); + + // Test with 3 eggs and 5 floors + assertEquals(3, minTrials(3, 5)); + } + + @Test + void testEdgeCases() { + // Test with single egg + assertEquals(0, minTrials(1, 0)); + assertEquals(1, minTrials(1, 1)); + assertEquals(2, minTrials(1, 2)); + + // Test with multiple eggs but minimal floors + assertEquals(0, minTrials(2, 0)); + assertEquals(1, minTrials(3, 1)); + } + + @Test + void testLargeInputs() { + assertEquals(4, minTrials(3, 10)); + assertEquals(7, minTrials(2, 36)); + } + + @Test + void testInvalidInputs() { + assertThrows(IllegalArgumentException.class, () -> minTrials(0, 5)); + assertThrows(IllegalArgumentException.class, () -> minTrials(-1, 5)); + assertThrows(IllegalArgumentException.class, () -> minTrials(2, -1)); } }