![]() |
VOOZH | about |
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 from a floor higher than f will break, and any egg dropped from or below floor f will not break. There are a few rules given below:
Your task is to find the minimum number of moves you need to determine the value of f with certainty.
Example:
Input: n = 2, k = 36
Output: 8
Explanation: In all the situations, 8 maximum moves are required to find the maximum floor. Following is the strategy to do so:
- Drop from floor 8 → If breaks, check 1-7 sequentially.
- Drop from floor 15 → If breaks, check 9-14.
- Drop from floor 21 → If breaks, check 16-20.
- Drop from floor 26 → If breaks, check 22-25.
- Drop from floor 30 → If breaks, check 27-29.
- Drop from floor 33 → If breaks, check 31-32.
- Drop from floor 35 → If breaks, check 34.
- Drop from floor 36 → Final check.
Input: n = 1, k = 36
Output: 36
Explanation: Drop the egg from the first-floor window; if it survives, drop it from the second-floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings.Input: n = 2, k = 10
Output: 4
Explanation: In all the situations, 4 maximum moves are required to find the maximum floor. Following is the strategy to do so:
- Drop from floor 4 → If breaks, check 1-3 sequentially.
- Drop from floor 7 → If breaks, check 5-6.
- Drop from floor 9 → If breaks, check 8.
- Drop from floor 10 → Final check.
Table of Content
The idea is to try dropping an egg from every floor (from 1 to K) and recursively calculate the minimum number of droppings needed in the worst case. To do so, run a loop from i equal to 1 to K, where i denotes the current floor. When we drop an egg from floor i, there are two possibilities:
- If the egg breaks: Then we need to check for floors lower than i with 1 less egg, i.e. the value of both egg and floor will be reduced by 1.
- If the egg doesn't break: Then we need to check for floors higher than i with same number of eggs, i.e. the number of floors to check will be k - i, and the count of eggs will remain same.
The maximum of these two move + 1(current move) will be considered, and the minimum of this is our answer.
For each recursive call, follow the below given steps:
4
In the recursive solution, many subproblems are computed more than once. Consider calculating the number of attempts for 2 eggs and 10 floors.
When dropping an egg from a certain floor i, the problem divides into two subproblems:
- one for the floors below (eggDrop(1, i-1))
- one for the floors above (eggDrop(2, 10-i)).
Different choices of i can lead to the same subproblem being solved multiple times (for example, eggDrop(2, 3) might be computed in several different branches).
This repeated computation of the same subproblems demonstrates overlapping subproblems, which makes memoization a highly effective optimisation technique.
The above approach can be optimized using memoization, as we are computing the same sub-problem multiple times.
- The idea is to create a 2d array memo[][] of order n * k, to store the results of the sub-problem, where memo[i][j] stores the result of i eggs and j floors.
- For each recursive call, check if the value is already computed, if so, return the stored value, else proceed similar to the above approach, and at last store the results in memo[][] and return the value.
4
A direct tabulation based solution for the above memoization and recursive approach would require O(n * k^2) Time and O(n * k) Space. We can further optimize the above approach.
In the above approach, each state memo[i][j] stores the number of moves required to solve the sub-problem of i eggs and j floors. Instead of doing so, the idea is to make dp[i][j] store the maximum number of floors that can be processed using i moves and j eggs. By doing so, we will only be required to find the maximum reachable floor in the previous move, and will avoid checking each k floor for all the states.
Step-by-step approach:
4
In the above approach, to calculate the current row of the dp[][] table, we require only the previous row results. The idea is to create a 1D array dp[] of size n to store the result for each move. To do so, proceed similar to above approach, and for each iteration, set dp[i] = 1 + dp[i] + dp[i-1]. At last return the value stored in dp[n].
Illustration for 2 Eggs and 10 Floors (n = 2, k = 10)
4
Related Articles: