Given N eggs and K floors, the task is to find the minimum number of trials needed, in the worst case, to find the floor below which all floors are safe. A floor is safe if dropping an egg from it does not break the egg. Please refer n eggs and k floors for more insight.
Examples:Â
Input: N = 2, K = 10Â
Output: 4Â
Explanation:Â
The first trial was on the 4th floor. Two cases arise after this:
- If the egg breaks, we have one egg left, so we need three more trials.
- If the egg does not break, the next try is from the 7th floor. Again, two cases arise.
Input: N = 2, K = 100Â
Output: 14
Prerequisites: Egg Dropping Puzzle
Approach: Consider this problem in a different way:Â
Let dp[x][n] is the maximum number of floors that can be checked with given n eggs and x moves.
Then the equation is:Â
- If the egg breaks, then we can check dp[x – 1][n – 1] floors.
- If the egg doesn’t break, then we can check dp[x – 1][n] + 1 floors.
Since we need to cover k floors, dp[x][n] >= k.
dp[x][n] is similar to the number of combinations and it increases exponentially to k
Below is the implementation of the above approach:Â Â
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; Â
// Function to return the minimum number // of trials needed in the worst case // with n eggs and k floors int eggDrop( int n, int k) { Â Â Â Â vector<vector< int > > dp(k + 1, vector< int >(n + 1, 0)); Â
    int x = 0; Â
    // Fill all the entries in table using     // optimal substructure property     while (dp[x][n] < k) { Â
        x++;         for ( int i = 1; i <= n; i++)             dp[x][i] = dp[x - 1][i - 1] + dp[x - 1][i] + 1;     } Â
    // Return the minimum number of moves     return x; } Â
// Driver code int main() { Â Â Â Â int n = 2, k = 36; Â
    cout << eggDrop(n, k); Â
    return 0; } |
Java
// Java implementation of the approach class GFG { Â Â Â Â Â // Function to return the minimum number // of trials needed in the worst case // with n eggs and k floors static int eggDrop( int n, int k) { Â Â Â Â int dp[][] = new int [k + 1 ][n + 1 ]; Â
    int x = 0 ; Â
    // Fill all the entries in table using     // optimal substructure property     while (dp[x][n] < k)     { Â
        x++;         for ( int i = 1 ; i <= n; i++)             dp[x][i] = dp[x - 1 ][i - 1 ] +                        dp[x - 1 ][i] + 1 ;     } Â
    // Return the minimum number of moves     return x; } Â
// Driver code public static void main(String args[]) { Â Â Â Â int n = 2 , k = 36 ; Â
    System.out.println( eggDrop(n, k)); } } Â
// This code is contributed by Arnab Kundu |
Python3
# Python implementation of the approach Â
# Function to return the minimum number # of trials needed in the worst case # with n eggs and k floors def eggDrop(n, k):     dp = [[ 0 for i in range (n + 1 )] for            j in range (k + 1 )] Â
    x = 0 ; Â
    # Fill all the entries in table using     # optimal substructure property     while (dp[x][n] < k): Â
        x + = 1 ;         for i in range ( 1 , n + 1 ):             dp[x][i] = dp[x - 1 ][i - 1 ] + \                         dp[x - 1 ][i] + 1 ;          # Return the minimum number of moves     return x; Â
# Driver code if __name__ = = '__main__' : Â Â Â Â n = 2 ; k = 36 ; Â
    print (eggDrop(n, k)); Â
# This code is contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using System; Â
class GFG { Â Â Â Â Â // Function to return the minimum number // of trials needed in the worst case // with n eggs and k floors static int eggDrop( int n, int k) { Â Â Â Â int [,]dp = new int [k + 1, n + 1]; Â
    int x = 0; Â
    // Fill all the entries in table using     // optimal substructure property     while (dp[x, n] < k)     { Â
        x++;         for ( int i = 1; i <= n; i++)             dp[x, i] = dp[x - 1, i - 1] +                     dp[x - 1, i] + 1;     } Â
    // Return the minimum number of moves     return x; } Â
// Driver code public static void Main(String []args) { Â Â Â Â int n = 2, k = 36; Â
    Console.WriteLine(eggDrop(n, k)); } } Â
// This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript implementation of the approach Â
// Function to return the minimum number // of trials needed in the worst case // with n eggs and k floors function eggDrop(n, k) { Â Â Â Â let dp = new Array(); Â
    for (let i = 0; i < k + 1; i++){         dp.push( new Array(n + 1).fill(0))     } Â
    let x = 0; Â
    // Fill all the entries in table using     // optimal substructure property     while (dp[x][n] < k) { Â
        x++;         for (let i = 1; i <= n; i++)             dp[x][i] = dp[x - 1][i - 1] + dp[x - 1][i] + 1;     } Â
    // Return the minimum number of moves     return x; } Â
// Driver code let n = 2, k = 36; document.write(eggDrop(n, k)); </script> |
8
Time Complexity: O(N * K)Â
Auxiliary Space: O(N * K)
Efficient Approach : Space Optimization
To optimize the space complexity of the above approach, we can observe that each entry dp[x][i] only depends on the previous row dp[x-1][i-1] and dp[x-1][i]. Therefore, we can use a 1D array instead of a 2D array to store the values of the current row and update it iteratively.
Step-by-step approach:
- Initialize a 1D vector dp of size n+1 with all elements set to 0.
- Initialize a variable x to 0.
- Enter a loop until the number of trials needed for n eggs is less than k.
- Inside the loop, iterate from n down to 1.
- Update the dp array at index i with the sum of the values at dp[i-1], dp[i], and 1.
- Increment x.
- Repeat the loop until the number of trials needed for n eggs is greater than or equal to k.
- Return the value of x.
Below is the implementation of the above approach:Â
C++
#include <bits/stdc++.h> using namespace std; Â
// Function to return the minimum number // of trials needed in the worst case // with n eggs and k floors int eggDrop( int n, int k) { Â Â Â Â vector< int > dp(n + 1, 0); Â
    int x = 0; Â
    // Fill the entries in the array     // using optimal substructure property     while (dp[n] < k) { Â
        x++;         for ( int i = n; i >= 1; i--)             dp[i] = dp[i - 1] + dp[i] + 1;     } Â
    // Return the minimum number of moves     return x; } Â
// Driver code int main() { Â Â Â Â int n = 2, k = 36; Â
    cout << eggDrop(n, k); Â
    return 0; } |
Python3
# Function to return the minimum number # of trials needed in the worst case # with n eggs and k floors def eggDrop(n, k):     dp = [ 0 ] * (n + 1 )     x = 0          # Fill the entries in the array     # using optimal substructure property     while dp[n] < k:         x + = 1         for i in range (n, 0 , - 1 ):             dp[i] = dp[i - 1 ] + dp[i] + 1          # Return the minimum number of moves     return x Â
n = 2 k = 36 print (eggDrop(n, k)) |
Javascript
// Function to return the minimum number // of trials needed in the worst case // with n eggs and k floors function eggDrop(n, k) { Â
    let dp = new Array(n + 1).fill(0);     let x = 0;               // Fill the entries in the array     // using optimal substructure property     while (dp[n] < k) {         x++;         for (let i = n; i >= 1; i--)             dp[i] = dp[i - 1] + dp[i] + 1;     }          // Return the minimum number of moves     return x; } Â
// Test case let n = 2, k = 36; console.log(eggDrop(n, k)); |
8
Time Complexity: O(N * K)Â
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!