You are given two identical eggs and you have access to a building with n
floors labeled from 1
to n
.
You know that there exists a floor f
where 0 <= f <= n
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.
In each move, you may take an unbroken egg and drop it from any floor x
(where 1 <= x <= n
). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
Return the minimum number of moves that you need to determine with certainty what the value of f
is.
Example 1:
Input: n = 2 Output: 2 Explanation: We can drop the first egg from floor 1 and the second egg from floor 2. If the first egg breaks, we know that f = 0. If the second egg breaks but the first egg didn't, we know that f = 1. Otherwise, if both eggs survive, we know that f = 2.
Example 2:
Input: n = 100 Output: 14 Explanation: One optimal strategy is: - Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting from floor 1 and going up one at a time to find f within 8 more drops. Total drops is 1 + 8 = 9. - If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9 and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more drops. Total drops is 2 + 12 = 14. - If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45, 55, 64, 72, 79, 85, 90, 94, 97, 99, and 100. Regardless of the outcome, it takes at most 14 drops to determine f.
Constraints:
1 <= n <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the egg drop problem is like trying every single floor as the first drop point. We want to find the worst-case scenario for each of these starting floors and then pick the best one from those worst cases.
Here's how the algorithm would work step-by-step:
def egg_drop_brute_force(number_of_floors):
minimum_number_of_drops = float('inf')
for floor_to_try_first in range(1, number_of_floors + 1):
# Calculate the worst-case number of drops for starting at this floor
worst_case_drops_for_floor = 0
# If the egg breaks on the first drop, we have to check all lower floors one by one.
if floor_to_try_first > 1:
worst_case_drops_for_floor = floor_to_try_first - 1
# If the egg does not break on the first drop we still need to account for
# the drop that was just performed
eggs_do_not_break = number_of_floors - floor_to_try_first + 1
if eggs_do_not_break > worst_case_drops_for_floor:
worst_case_drops_for_floor = eggs_do_not_break
# Update the overall minimum if this floor's worst case is better.
if worst_case_drops_for_floor < minimum_number_of_drops:
minimum_number_of_drops = worst_case_drops_for_floor
return minimum_number_of_drops
The goal is to find the highest floor from which an egg won't break, minimizing the worst-case number of drops. The key insight is to make each drop potentially eliminate an equal number of floors below, gradually narrowing down the possibilities.
Here's how the algorithm would work step-by-step:
def egg_drop(number_of_floors):
increment_value = 1
total_drops = 0
current_floor = 0
# Determine drops in worst case
while current_floor < number_of_floors:
total_drops += 1
current_floor += increment_value
increment_value += 1
return total_drops
def egg_drop_two_eggs(number_of_floors):
drops_needed = 0
increment = 1
#Determines the minimum number of drops.
while number_of_floors > 0:
number_of_floors -= increment
increment += 1
drops_needed += 1
return drops_needed
def egg_drop_two_eggs_detailed(number_of_floors):
drops_needed = 0
increment_value = egg_drop(number_of_floors) # Initial Increment Calculation
current_floor = 0
while current_floor < number_of_floors:
drops_needed += 1
current_floor += increment_value
# Egg breaks, test floors below.
if current_floor > number_of_floors:
drops_needed += (current_floor - number_of_floors) #worst case linear search
break
increment_value -= 1
return drops_needed
Case | How to Handle |
---|---|
N = 0 floors | Return 0 because no drops are needed for 0 floors. |
N = 1 floor | Return 1 because a single drop on the first floor determines the critical floor. |
Two eggs and very large N (e.g., N > 10000) | Ensure the algorithm's space and time complexity remain within acceptable bounds, avoiding excessive memory usage or slow execution. |
Integer overflow when calculating the drop interval | Use appropriate data types (e.g., long) or techniques to prevent integer overflows during calculations. |
Critical floor is the first floor | The algorithm should correctly identify this case and return 1. |
Critical floor is the Nth floor | The algorithm should correctly identify this case and return the minimum number of drops needed in the worst case. |
No critical floor exists (egg never breaks) | Handle this case by correctly reporting the number of drops when the first egg breaks on the Nth floor. |
N is negative | Treat this as an invalid input and return an error or a default value like -1. |