diff --git a/DIRECTORY.md b/DIRECTORY.md index 0e3dd66a6bc6..49fac18a6716 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -445,6 +445,7 @@ * [Climbing Stairs](dynamic_programming/climbing_stairs.py) * [Combination Sum Iv](dynamic_programming/combination_sum_iv.py) * [Edit Distance](dynamic_programming/edit_distance.py) + * [Egg Drop](dynamic_programming/egg_drop.py) * [Egg Dropping](dynamic_programming/egg_dropping.py) * [Factorial](dynamic_programming/factorial.py) * [Fast Fibonacci](dynamic_programming/fast_fibonacci.py) diff --git a/dynamic_programming/egg_drop.py b/dynamic_programming/egg_drop.py new file mode 100644 index 000000000000..f6cad21ce4b7 --- /dev/null +++ b/dynamic_programming/egg_drop.py @@ -0,0 +1,46 @@ +""" +Egg Dropping Problem is a well-known problem in computer science. +The task is to find the minimum number of attempts required in the +worst case to find the highest floor from which an egg can be dropped +without breaking, given a certain number of floors and eggs. + +Wikipedia: https://en.wikipedia.org/wiki/Dynamic_programming#Egg_dropping_puzzle +""" + + +def egg_drop(eggs: int, floors: int) -> int: + """ + Calculate the minimum number of attempts required in the worst case + to determine the highest floor from which an egg can be dropped + without breaking it. + + Parameters: + eggs (int): Number of eggs available. + floors (int): Number of floors to test. + + Returns: + int: Minimum number of attempts required in the worst case. + + Example: + >>> egg_drop(2, 10) + 4 + + >>> egg_drop(3, 14) + 4 + """ + # Initialize dp table with integers + dp = [[0 for _ in range(floors + 1)] for _ in range(eggs + 1)] + + # Fill dp table for one egg (we have to try all floors) + for i in range(1, floors + 1): + dp[1][i] = i + + # Fill the rest of the dp table + for e in range(2, eggs + 1): + for f in range(1, floors + 1): + dp[e][f] = floors + 1 # Start with an arbitrary large integer + for k in range(1, f + 1): + res = 1 + max(dp[e - 1][k - 1], dp[e][f - k]) + dp[e][f] = min(dp[e][f], res) + + return dp[eggs][floors]