Problem Statement

Suppose you have N eggs and you want to determine from which floor in a K-floor building you can drop an egg such that it doesn't break. You have to determine the minimum number of attempts you need in order find the critical floor in the worst case while using the best strategy.There are few rules given below.

For more description on this problem see wiki page

Example 1:

Input:
N = 2, K = 10
Output:4

Example 2:

Input:N = 3, K = 5
Output:3

Problem Link

Egg Dropping Puzzle | Practice | GeeksforGeeks

Reference Videos

https://www.youtube.com/watch?v=UvksR0hR9nA&ab_channel=Pepcoding

Code

class Solution
{
    public:
    //Function to find minimum number of attempts needed in 
    //order to find the critical floor.
    int eggDrop(int n, int k) 
    {
        vector<vector<int>> dp(n+1,vector<int>(k+1));
        
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=k;j++)
            {
                if(i==0&&j==0)//Invalid case
                dp[i][j] = 0;
                
                else if(i==0&&j)//No eggs so infinite tries to find critical floor
                dp[i][j] = INT_MAX;
                
                else if(i&&!j)//No floors exists so critical floor doesn't exists so 0 tries
                dp[i][j] = 0;
                
                else if(j==1&&i)//1 floor exists so only 1 try is required to find critical floor
                dp[i][j] = 1;
                
                else if(i==1&&j)//Having only 1 egg so j tries to find if total j floors are there
                dp[i][j] = j;
                
                else
                {
                    dp[i][j] = INT_MAX;
                    for(int f=1;f<=j;f++)
                    {
                        
                        int temp = 1+max(dp[i-1][f-1],dp[i][j-f]);
                        dp[i][j] = min(dp[i][j],temp);
                    }
                }
            }
            
            
        }
        
        return dp[n][k];
    }
};
//Using Bottom-Up DP

class Solution
{
    public:
    //Function to find minimum number of attempts needed in 
    //order to find the critical floor.
    int dp[201][201];
    int eggDrop(int n, int k) 
    {
       vector<vector<int>> dp(n+1,vector<int>(k+1));
       
       for(int i=0;i<=n;i++) {
           for(int j=0;j<=k;j++) {
               if(i==0)
               dp[i][j] = INT_MAX;
               
               else if(i==1||j==1||j==0)
               dp[i][j] = j;
               
               else {
                   
                    dp[i][j] = INT_MAX;
                    for(int k=1;k<=j;k++)
                    {
                        int temp = 1 + max(dp[i-1][k-1],dp[i][j-k]);
                        dp[i][j] = min(dp[i][j],temp);
                    }
               }
           }
       }
        
        return dp[n][k];
    }
    
};
// Time Complexity : O(n^3),   Space Complexity : O(n^2)
// Time Optimized Bottom Up DP

class Solution {
public:
    int superEggDrop(int K, int N) {
        
        int dp[K+1][N+1];
        
        for(int i=1;i<=K;i++)
        {
            for(int j=0;j<=N;j++)
            {
                if(i==1)
                    dp[i][j] = j;
                
                else if(j==1)
                    dp[i][j] = 1;
                
                else if(j==0)
                    dp[i][j] = 0;
                
                else
                {   dp[i][j] = INT_MAX;
                    int l = 1, h = j, temp=0;
                    while(l<=h) {
                        int m = (l+h)/2;
                        
                        int left = dp[i-1][m-1]; //egg broken check for down floors of mid
                        int right = dp[i][j-m]; // not broken check for up floors of mid
                        temp = 1 + max(left,right);  //store max of both 
                        
                        if(left<right)  //since right is more than left and we need more in worst case 
                            l = m+1;   // so l=mid+1 to gain more temp for worst case : upward
                            
                         
                        else   //left > right so we will go downward 
                            h = m-1;
                     
                    dp[i][j] = min(dp[i][j],temp);  //store minimum attempts
                    }
                 
                 
                }
            }
        }
        // cout<<endl;
        return dp[K][N];
    }
};
// Time Complexity : O(n^2*logn),   Space Complexity : O(n^2)