|
1 | 1 | package com.thealgorithms.dynamicprogramming;
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * DynamicProgramming solution for the Egg Dropping Puzzle |
| 4 | + * Dynamic Programming solution for the Egg Dropping Puzzle |
| 5 | + * The problem is to find the minimum number of attempts needed in the worst case to find the critical |
| 6 | + * floor from which if an egg is dropped, it will break. |
| 7 | + * Time Complexity: O(n * m * m), where n is number of eggs and m is number of floors |
| 8 | + * Space Complexity: O(n * m) to store the DP table |
5 | 9 | */
|
6 | 10 | public final class EggDropping {
|
| 11 | + |
7 | 12 | private EggDropping() {
|
| 13 | + // private constructor to prevent instantiation |
8 | 14 | }
|
9 |
| - |
10 |
| - // min trials with n eggs and m floors |
11 |
| - public static int minTrials(int n, int m) { |
12 |
| - int[][] eggFloor = new int[n + 1][m + 1]; |
13 |
| - int result; |
14 |
| - int x; |
15 |
| - |
16 |
| - for (int i = 1; i <= n; i++) { |
17 |
| - eggFloor[i][0] = 0; // Zero trial for zero floor. |
18 |
| - eggFloor[i][1] = 1; // One trial for one floor |
| 15 | + |
| 16 | + /** |
| 17 | + * Finds minimum number of trials needed in worst case for n eggs and m floors |
| 18 | + * |
| 19 | + * @param eggs The number of eggs available |
| 20 | + * @param floors The number of floors in the building |
| 21 | + * @return Minimum number of trials needed in worst case |
| 22 | + * @throws IllegalArgumentException if eggs <= 0 or floors < 0 |
| 23 | + */ |
| 24 | + public static int minTrials(int eggs, int floors) { |
| 25 | + if (eggs <= 0 || floors < 0) { |
| 26 | + throw new IllegalArgumentException("Number of eggs must be positive and floors must be non-negative"); |
19 | 27 | }
|
20 | 28 |
|
21 |
| - // j trials for only 1 egg |
22 |
| - for (int j = 1; j <= m; j++) { |
23 |
| - eggFloor[1][j] = j; |
| 29 | + // dp[i][j] represents minimum number of trials needed for i eggs and j floors |
| 30 | + int[][] dp = new int[eggs + 1][floors + 1]; |
| 31 | + |
| 32 | + // Base case 1: Zero trials for zero floor |
| 33 | + // Base case 2: One trial for one floor |
| 34 | + for (int i = 1; i <= eggs; i++) { |
| 35 | + dp[i][0] = 0; |
| 36 | + dp[i][1] = 1; |
24 | 37 | }
|
25 |
| - |
26 |
| - // Using bottom-up approach in DP |
27 |
| - for (int i = 2; i <= n; i++) { |
28 |
| - for (int j = 2; j <= m; j++) { |
29 |
| - eggFloor[i][j] = Integer.MAX_VALUE; |
30 |
| - for (x = 1; x <= j; x++) { |
31 |
| - result = 1 + Math.max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]); |
32 |
| - |
33 |
| - // choose min of all values for particular x |
34 |
| - if (result < eggFloor[i][j]) { |
35 |
| - eggFloor[i][j] = result; |
36 |
| - } |
| 38 | + |
| 39 | + // Base case 3: With one egg, need to try every floor from bottom |
| 40 | + for (int j = 1; j <= floors; j++) { |
| 41 | + dp[1][j] = j; |
| 42 | + } |
| 43 | + |
| 44 | + // Fill rest of the entries in table using optimal substructure property |
| 45 | + for (int i = 2; i <= eggs; i++) { |
| 46 | + for (int j = 2; j <= floors; j++) { |
| 47 | + dp[i][j] = Integer.MAX_VALUE; |
| 48 | + // Try dropping egg from each floor and find minimum trials needed |
| 49 | + for (int k = 1; k <= j; k++) { |
| 50 | + // Maximum of: |
| 51 | + // 1) Egg breaks at floor k: Check below floors with i-1 eggs |
| 52 | + // 2) Egg doesn't break: Check above floors with i eggs |
| 53 | + int attempts = 1 + Math.max(dp[i - 1][k - 1], dp[i][j - k]); |
| 54 | + dp[i][j] = Math.min(dp[i][j], attempts); |
37 | 55 | }
|
38 | 56 | }
|
39 | 57 | }
|
40 |
| - |
41 |
| - return eggFloor[n][m]; |
| 58 | + |
| 59 | + return dp[eggs][floors]; |
42 | 60 | }
|
43 |
| - |
| 61 | + |
| 62 | + /** |
| 63 | + * Example usage |
| 64 | + */ |
44 | 65 | public static void main(String[] args) {
|
45 |
| - int n = 2; |
46 |
| - int m = 4; |
47 |
| - // result outputs min no. of trials in worst case for n eggs and m floors |
48 |
| - int result = minTrials(n, m); |
49 |
| - System.out.println(result); |
| 66 | + try { |
| 67 | + // Example: 2 eggs and 4 floors |
| 68 | + System.out.println("Minimum number of trials in worst case with 2 eggs and 4 floors: " |
| 69 | + + minTrials(2, 4)); |
| 70 | + |
| 71 | + // Additional test case |
| 72 | + System.out.println("Minimum number of trials in worst case with 3 eggs and 5 floors: " |
| 73 | + + minTrials(3, 5)); |
| 74 | + } catch (IllegalArgumentException e) { |
| 75 | + System.err.println("Error: " + e.getMessage()); |
| 76 | + } |
50 | 77 | }
|
51 | 78 | }
|
0 commit comments