"""
Calculate the minimum number of attempts needed in the worst case to find the
critical floor from which eggs start breaking when dropped.
"""
def egg_dropping(eggs: int, floors: int) -> int:
"""
Calculate the minimum number of attempts needed in the worst case for `eggs`
and `floors` using dynamic programming.
>>> egg_dropping(1, 5)
5
>>> egg_dropping(2, 6)
3
>>> egg_dropping(2, 10)
4
"""
if floors in (0, 1):
return floors
if eggs == 1:
return floors
dp = [[0 for _ in range(floors + 1)] for _ in range(eggs + 1)]
for i in range(1, floors + 1):
dp[1][i] = i
for e in range(2, eggs + 1):
for f in range(1, floors + 1):
dp[e][f] = 10**9
for x in range(1, f + 1):
res = 1 + max(dp[e - 1][x - 1], dp[e][f - x])
dp[e][f] = min(dp[e][f], res)
return dp[eggs][floors]
if __name__ == "__main__":
print("\n********* Egg Dropping Problem Using Dynamic Programming ************\n")
print("\n*** Enter -1 at any time to quit ***")
print("\nEnter the number of eggs and floors separated by a space: ", end="")
try:
while True:
input_data = input().strip()
if input_data == "-1":
print("\n********* Goodbye!! ************")
break
else:
eggs, floors = map(int, input_data.split())
print(
f"The minimum number of attempts required with {eggs} eggs and "
f"{floors} floors is:"
)
print(egg_dropping(eggs, floors))
print("Try another combination of eggs and floors: ", end="")
except NameError, ValueError:
print("\n********* Invalid input, goodbye! ************\n")