Egg Dropping Puzzle | Practice | GeeksforGeeks

You are given N identical eggs and you have access to a K-floored building from 1 to K.
There exists a floor f where 0 <= f <= K such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break. There are few rules given below. 
An egg that survives a fall can be used again.
A broken egg must be discarded.
The effect of a fall is the same for all eggs.
If the egg doesn't break at a certain floor, it will not break at any floor below.
If the eggs breaks at a certain floor, it will break at any floor above.
Return the minimum number of moves that you need to determine with certainty what the value of f is.
For more description on this problem see wiki page
Example 1:
Input:
N = 1, K = 2
Output: 2
Explanation: 
1. Drop the egg from floor 1. If it 
   breaks, we know that f = 0.
2. Otherwise, drop the egg from floor 2.
   If it breaks, we know that f = 1.
3. If it does not break, then we know f = 2.
4. Hence, we need at minimum 2 moves to
   determine with certainty what the value of f is.
Example 2:
Input:
N = 2, K = 10
Output: 4
Your Task:
Complete the function eggDrop() which takes two positive integer N and K as input parameters and returns the minimum number of attempts you need in order to find the critical floor.
Expected Time Complexity : O(N*(K^2))
Expected Auxiliary Space: O(N*K)
Constraints:
1<=N<=200
1<=K<=200
0
mayanksh4rmain 1 hour
GETTING SEGFAULT, PLEASE HELP:
class Solution
{
    public:
    vector<vector<int>> dp;
    
    int solve(int eggs, int floors){
        if(floors == 0 or floors == 1){
            return floors;
        }
        
        if(eggs == 1){
            return floors;
        }
        
        if(dp[eggs][floors] != -1){
            return dp[eggs][floors];
        }
        
        int ans = INT_MAX;
        
        for(int currentFloor = 1; currentFloor <= floors; currentFloor++){
             int temp = 1 + max(solve(eggs-1, currentFloor - 1), solve(eggs, floors - currentFloor));
            
            int ans = min(ans, temp);
        }
        
        dp[eggs][floors] = ans;
        
        return dp[eggs][floors];
    }
    
    int eggDrop(int n, int k) 
    {
        dp.resize(n, vector<int>(k, -1));
        return solve(n, k);
    }
};
+1
shivamshuklashivam295 days ago
class Solution
{
    public:
    int arr[202][202];
    Solution(){
        memset(arr , -1 , sizeof(arr));
    }
    int eggDrop(int e, int f) 
    {   
     
        if(f == 0 || f == 1) return f;
        if(e == 1)  return f;
      
        if(arr[e][f] != -1) return arr[e][f];
        
        int ans = INT_MAX;
        for(int k=1;k<=f;k++){
            int temp = 1+ max(eggDrop(e-1,k-1) , eggDrop(e , f-k));
            if(temp < ans) ans = temp;
        }
        return arr[e][f] = ans;
    }
};
+1
vinamrajha5 days ago
int eggDrop(int n, int k) 
   {
       // your code here
       int dp[k+1][n+1];
       for(int i =1; i<=n; ++i){
           dp[0][i] =0;
           dp[1][i] =1;
       }
       for(int i =1; i<=k; ++i)dp[i][1] = i;
       for(int i =2; i<=k; ++i){
           for(int j =2; j<=n; ++j){
               dp[i][j] = INT_MAX;
               for(int x =1; x<=i; ++x) dp[i][j] = min(dp[i][j], 1+max(dp[x-1][j-1], dp[i-x][j]));
           }
       }
       return dp[k][n];
   }
0
milindprajapatmst196 days ago
0.08/1.07 
const int N = 200;
int dp[N + 1][N + 1];

class Solution {
    public:
    int eggDrop(int n, int k) {
        if (k == 0 || k == 1 || n == 1)
            return k;
        if (dp[n][k] == -1) {
            int result = INT_MAX;
            for (int f = 1; f <= k; f++)
                result = min(result, 1 + max(eggDrop(n - 1, f - 1), eggDrop(n, k - f)));
            dp[n][k] = result;
        }
        return dp[n][k];
    }
    Solution() {
        memset(dp, -1, sizeof(dp));
    }
};
0
divyansh1923cse10022 weeks ago
Why TLE in python Code?
import sys
class Solution:
    
    #Function to find minimum number of attempts needed in 
    #order to find the critical floor.
    def eggDrop(self,n, k):
        dp=[[-1 for i in range(200)] for j in range(200)]
        if k==0 or k==1:
            return k
        mn=sys.maxsize
        if n==1:
            return k
        if dp[n][k]!=-1:
            return dp[k][n]
        for t in range(1,k):
            if dp[n-1][t-1]!=-1:
                left=dp[n-1][t-1]
            else:
                left=self.eggDrop(n-1,t-1)
                dp[n-1][t-1]=left
            if dp[n][k-t]!=-1:
                right=dp[n][k-t]
            else:
                right=self.eggDrop(n,k-t)
                dp[n][k-t]=right
            temp=1+max(left,right)
            mn=min(temp,mn)
        dp[n][k]=mn
        return dp[n][k]
0
jainmuskan5652 weeks ago
// MCM variation question   int solve(int egg,int flr){       // starting floor base condition       if(flr==1 || flr==0){           return flr;       }       // if only 1 egg we start from bottom floor until f where it breaks for the first time       if(egg==1){           return flr;       }       // memoization       if(dp[egg][flr]!= -1){           return dp[egg][flr];       }       int ans= INT_MAX;       for(int k=1;k<=flr;k++){           // check worst case thats why max           // if egg breaks then egg-1 and floor till kth floor           // if it doesn't break then egg and floor above k (floor-k) would be 1st floor for next recursion                      int temp= 1+max(solve(egg-1,k-1),solve(egg,flr-k));           // min of all the worst maxes           ans= min(ans,temp);       }       return dp[egg][flr]= ans;          }   int eggDrop(int n, int k)    {       memset(dp,-1,sizeof(dp));       return solve(n,k);   }
0
ashutosh0920002 weeks ago
class Solution {   //Function to find minimum number of attempts needed in    //order to find the critical floor.   static int eggDrop(int n, int k) {    int[][] dp = new int [n+1][k+1];    for(int i=1;i<=n;i++){        for(int j=1;j<=k;j++){            if(i==1){                dp[i][j] = j ;             }            else if(j==1){                dp[i][j]=1;            }            else{                int min = Integer.MAX_VALUE;                for(int mj = j-1,pj=0;mj>=0;mj--,pj++){                    int val1 = dp[i][mj];//egg survives                    int val2 = dp[i-1][pj];//egg breaks                    int val = Math.max(val1,val2);                    min = Math.min(val,min);                }                dp[i][j] = min+1;            }        }    }    return dp[n][k];}} 
0
ashutosh092000
This comment was deleted.
0
aishwaryadwani97993 weeks ago
class Solution {
    static int t[][] = new int[201][201];
 
    static int solve(int e, int f) {
        if (f == 0 || f == 1 || e == 1)
            return f;
        if (t[e][f] != -1)
            return t[e][f];
        int ans = Integer.MAX_VALUE;
        for (int k = 1; k <= f; k++) {
            int tempAns = 1 + Math.max(solve(e - 1, k - 1), solve(e, f - k));
            ans = Math.min(ans, tempAns);
        }
        return t[e][f] = ans;
    }
 
    static int eggDrop(int e, int f) {
        for (int arr[] : t) {
            Arrays.fill(arr, -1);
        }
        return solve(e, f);
    }
}
0
shrustis1764 weeks ago
C++ solution (fully optimised)
 
int t[201][201];      int solve( int e, int f)   {       if( f==0 || f==1)           return f;       if(e==1)           return f;              if(t[e][f] != -1)           return t[e][f];       int mn = INT_MAX;       int low, high;              for(int k=1; k<=f; k++)       {           if(t[e-1][k-1] != -1)           {                low = t[e-1][k-1];           }           else           {               low=solve( e-1, k-1);               t[e-1][k-1] = low;           }                      if(t[e][f-k] != -1)           {                high = t[e][f-k];           }           else           {               high =solve( e, f-k);               t[e][f-k] = high;           }                      int temp = 1+max(low,high);                      mn = min(mn, temp);       }       return t[e][f] =mn;   }   int eggDrop(int n, int k)    {       // your code here       memset(t,-1,sizeof(t));              return solve(n,k);          }}; 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.