Egg Drop With 2 Eggs And N Floors

Solution For Egg Drop With 2 Eggs And N Floors

Problem:

You are given two identical eggs and you have access to a building with n floors labeled from 1 to n.

You also have a function called drop that takes an integer input of a floor and returns:

0 if the egg does not break on that floor
1 if the egg breaks on that floor
You have to use the eggs to find the minimum number of floors it will take to determine with certainty the value of f such that 0 <= f <= n.

Approach:

This problem belongs to dynamic programming and can be solved via memoization technique.

We need to first create a matrix memo of dimensions n+1 and 3+1.

Here, memo[i][j] represents the minimum number of moves needed to determine the value of f when we have i floors to work with and j eggs.

At first, we need to fill the first row in which we have only one egg. So, we will fill the matrix as follows:

memo[1][1] = 1
memo[2][1] = 2
memo[3][1] = 3
.
.
.
memo[n][1] = n

For eg: When we have only one egg and i floors, we start dropping the egg from 1st floor, then 2nd and so on till we find a floor from which egg broke. Hence, the minimum number of moves will be equal to the number of floors we have in this case.

Now, we have to consider the second egg. We can’t use the same approach we did for the first egg. If we start from the first floor and move up floor by floor, it will take a lot of time and we might run out of eggs before finding out the floor f. So, we need a better approach.

If we drop an egg from a given floor, then there are two possibilities:

Case 1: The egg breaks and we are left with j-1 eggs to test for the remaining i-1 floors below that floor.
Case 2: The egg does not break and we have j eggs to test for the remaining floors above the current floor.

So, if we drop an egg from a certain floor, then the minimum number of moves required will be equal to the maximum of the following:

  1. 1 + memo[i – 1][j – 1] (If the egg breaks, check from floors 1 to i-1, and i-1 floors remain with j-1 eggs)
  2. 1 + memo[n – i]j

We have to take the minimum number of moves as we want to minimize the worst case and find the value of f with certainty.

Solution:

class Solution:
def twoEggDrop(self, n: int) -> int:

    memo = [[float('inf') for j in range(4)] for i in range(n + 1)]

    #filling the first column
    for i in range(n+1):
        memo[i][1] = i

    #filling the first row
    for i in range(1,4):
        memo[1][i] = 1

    for i in range(2,n+1):
        for j in range(2,4):
            for k in range(1,i):
                memo[i][j] = min(memo[i][j], 1+max(memo[k-1][j-1],memo[i-k][j]))

    return memo[n][2]  #returning minimum number of moves with 2 eggs and n floors.

Time Complexity: O(n^2)
Space Complexity: O(n * 4)

Step by Step Implementation For Egg Drop With 2 Eggs And N Floors

There are 2 eggs and n floors.

We can drop an egg from a given floor x and it will either break or not.

If the egg breaks, we will have to check the floors below x to see if the other egg can survive a fall from there.

If the egg doesn't break, we can check the floors above x to see if the other egg can survive a fall from there.

If both eggs break, then we know that the floor we are on is not safe.

If only one egg breaks, then we can check if the other egg can survive a fall from the next floor up or down.

If neither egg breaks, then we can check if the other egg can survive a fall from the next floor up or down.

We can keep track of which floors are safe and which are not.

If both eggs break on the same floor, then that floor is not safe.

If only one egg breaks, then the other egg can be used to check the next floor up or down.

If neither egg breaks, then we can check the next floor up or down.
# A building has 100 floors. One of the floors is the highest floor an egg can be dropped from without breaking.
# 
# If an egg is dropped from above that floor, it will break. If it is dropped from that floor or below, it will be unbroken.
# 
# Given 2 eggs, find the highest floor an egg can be dropped from without breaking, with as few drops as possible.

# This is a classic computer science problem known as the "egg drop" problem.

# We can solve this problem with a dynamic programming approach.

# First, we define a function f(n, k) which represents the minimum number of drops needed to find the critical floor when we have n eggs and k floors.

# If we only have one egg, the best we can do is drop it from each floor until it breaks, so f(1, k) = k.

# If we have two eggs, we can drop the first egg from the first floor. If it breaks, then we know that the critical floor is somewhere between the first and second floors, so we can use the second egg to drop from the second floor until it either breaks or we find the critical floor.

# If the first egg doesn't break, then we can drop it from the second floor. If it breaks, then we know that the critical floor is somewhere between the first and second floors, so we can use the second egg to drop from the first floor until it either breaks or we find the critical floor.

# In either case, the number of drops needed is 1 + f(1, k-1), so we have the following recursive relation:

# f(2, k) = 1 + min(f(1, k-1), f(2, k-1))

# We can use this recursive relation to fill in a table of values for f(n, k), which we can then use to find the desired result.

# The following is Python code for the egg drop problem:

def egg_drop(n, k):
    # Base case: one egg
    if n == 1:
        return k
    
    # Base case: no floors
    if k == 0:
        return 0
    
    # Recursive case: two or more eggs
    min_drops = float('inf')
    for x in range(1, k+1):
        min_drops = min(min_drops, 1 + max(egg_drop(n-1, x-1), egg_drop(n, k-x)))
        
    return min_drops
// There are two eggs and n floors. 
// We need to find the minimum number of trials needed to find the critical floor. 
// In every trial, we can either drop an egg from the current floor or move up one floor. 

// If the egg breaks, we have to start the trials again from the next floor. 
// If the egg doesn't break, we can move on to the next floor until we reach the end. 

// We need to find the minimum number of trials needed to find the critical floor. 

// For example, if n = 10, 
// we need to find the minimum number of trials needed to find the critical floor. 

// The critical floor is the floor where the egg will break. 

// We can start from the first floor and drop the egg. 
// If it doesn't break, we can move on to the next floor and drop the egg again. 
// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We can create an array to store the minimum number of trials needed for each floor. 

// For example, if n = 10, 

// the array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

// We can start from the first floor and drop the egg. 

// If it doesn't break, we can move on to the next floor and drop the egg again. 

// If it breaks, we need to start the trials again from the next floor. 

// We can keep track of the number of trials needed to find the critical floor. 

// In the worst case, we need to try n times. 

// We can use a dynamic programming approach to solve this problem. 

// We
#include  
using namespace std; 
  
int eggDrop(int n, int k) 
{ 
    // If there are no floors, then no trials needed. OR if there is 
    // one floor, one trial needed. 
    if (k == 1 || k == 0) 
        return k; 
  
    // We need k trials for one egg and k floors 
    if (n == 1) 
        return k; 
  
    int min = INT_MAX, x, res; 
  
    // Consider all droppings from 1st floor to kth floor and 
    // return the minimum of these values plus 1. 
    for (x = 1; x <= k; x++) 
    { 
        res = max(eggDrop(n-1, x-1), eggDrop(n, k-x)); 
        if (res < min) 
            min = res; 
    } 
  
    return min + 1; 
} 
  
/* Driver program to test to pront printTable() */
int main() 
{ 
    int n = 2, k = 36; 
    printf ("\nMinimum number of trials in worst case with %d eggs and "
             "%d floors is %d \n", n, k, eggDrop(n, k)); 
    return 0; 
}
using System; 

public class EggDrop 
{ 
    // A utility function to get maximum of two integers 
    static int Max(int a, int b) { return (a > b)? a: b; } 
  
    /* Function to get minimum number of trials needed in worst 
    case with n eggs and k floors */
    static int eggDrop(int n, int k) 
    { 
        /* A 2D table where entery eggFloor[i][j] will represent minimum 
        number of trials needed for i eggs and j floors. */
        int eggFloor[][] = new int[n+1][k+1]; 
        int res; 
        int i, j, x; 
  
        // We need one trial for one floor and0 trials for 0 floors 
        for (i = 1; i <= n; i++) 
        { 
            eggFloor[i][1] = 1; 
            eggFloor[i][0] = 0; 
        } 
  
        // We always need j trials for one egg and j floors. 
        for (j = 1; j <= k; j++) 
            eggFloor[1][j] = j; 
  
        // Fill rest of the entries in table using optimal substructure 
        // property 
        for (i = 2; i <= n; i++) 
        { 
            for (j = 2; j <= k; j++) 
            { 
                eggFloor[i][j] = Int32.MaxValue; 
                for (x = 1; x <= j; x++) 
                { 
                     res = 1 + Max(eggFloor[i-1][x-1], eggFloor[i][j-x]); 
                     if (res < eggFloor[i][j]) 
                        eggFloor[i][j] = res; 
                } 
            } 
        } 
  
        // eggFloor[n][k] holds the result 
        return eggFloor[n][k]; 
    } 
  
    /* Driver program to test to pront printTable*/
    public static void Main() 
    { 
        int n = 2, k = 36; 
        Console.Write("Minimum number of trials in worst case with "+ 
                       n + " eggs and "+ k + 
                       " floors is "+ eggDrop(n, k)); 
    } 
}


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]