mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-10-06 05:39:30 +00:00
Compare commits
3 Commits
740bffecf4
...
ef63906bf5
Author | SHA1 | Date | |
---|---|---|---|
|
ef63906bf5 | ||
|
9a572dec2b | ||
|
677abbab38 |
46
dynamic_programming/egg_drop.py
Normal file
46
dynamic_programming/egg_drop.py
Normal file
|
@ -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]
|
|
@ -7,6 +7,8 @@ the Binet's formula function because the Binet formula function uses floats
|
|||
|
||||
NOTE 2: the Binet's formula function is much more limited in the size of inputs
|
||||
that it can handle due to the size limitations of Python floats
|
||||
NOTE 3: the matrix function is the fastest and most memory efficient for large n
|
||||
|
||||
|
||||
See benchmark numbers in __main__ for performance comparisons/
|
||||
https://en.wikipedia.org/wiki/Fibonacci_number for more information
|
||||
|
@ -17,6 +19,9 @@ from collections.abc import Iterator
|
|||
from math import sqrt
|
||||
from time import time
|
||||
|
||||
import numpy as np
|
||||
from numpy import ndarray
|
||||
|
||||
|
||||
def time_func(func, *args, **kwargs):
|
||||
"""
|
||||
|
@ -230,6 +235,88 @@ def fib_binet(n: int) -> list[int]:
|
|||
return [round(phi**i / sqrt_5) for i in range(n + 1)]
|
||||
|
||||
|
||||
def matrix_pow_np(m: ndarray, power: int) -> ndarray:
|
||||
"""
|
||||
Raises a matrix to the power of 'power' using binary exponentiation.
|
||||
|
||||
Args:
|
||||
m: Matrix as a numpy array.
|
||||
power: The power to which the matrix is to be raised.
|
||||
|
||||
Returns:
|
||||
The matrix raised to the power.
|
||||
|
||||
Raises:
|
||||
ValueError: If power is negative.
|
||||
|
||||
>>> m = np.array([[1, 1], [1, 0]], dtype=int)
|
||||
>>> matrix_pow_np(m, 0) # Identity matrix when raised to the power of 0
|
||||
array([[1, 0],
|
||||
[0, 1]])
|
||||
|
||||
>>> matrix_pow_np(m, 1) # Same matrix when raised to the power of 1
|
||||
array([[1, 1],
|
||||
[1, 0]])
|
||||
|
||||
>>> matrix_pow_np(m, 5)
|
||||
array([[8, 5],
|
||||
[5, 3]])
|
||||
|
||||
>>> matrix_pow_np(m, -1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: power is negative
|
||||
"""
|
||||
result = np.array([[1, 0], [0, 1]], dtype=int) # Identity Matrix
|
||||
base = m
|
||||
if power < 0: # Negative power is not allowed
|
||||
raise ValueError("power is negative")
|
||||
while power:
|
||||
if power % 2 == 1:
|
||||
result = np.dot(result, base)
|
||||
base = np.dot(base, base)
|
||||
power //= 2
|
||||
return result
|
||||
|
||||
|
||||
def fib_matrix_np(n: int) -> int:
|
||||
"""
|
||||
Calculates the n-th Fibonacci number using matrix exponentiation.
|
||||
https://www.nayuki.io/page/fast-fibonacci-algorithms#:~:text=
|
||||
Summary:%20The%20two%20fast%20Fibonacci%20algorithms%20are%20matrix
|
||||
|
||||
Args:
|
||||
n: Fibonacci sequence index
|
||||
|
||||
Returns:
|
||||
The n-th Fibonacci number.
|
||||
|
||||
Raises:
|
||||
ValueError: If n is negative.
|
||||
|
||||
>>> fib_matrix_np(0)
|
||||
0
|
||||
>>> fib_matrix_np(1)
|
||||
1
|
||||
>>> fib_matrix_np(5)
|
||||
5
|
||||
>>> fib_matrix_np(10)
|
||||
55
|
||||
>>> fib_matrix_np(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: n is negative
|
||||
"""
|
||||
if n < 0:
|
||||
raise ValueError("n is negative")
|
||||
if n == 0:
|
||||
return 0
|
||||
|
||||
m = np.array([[1, 1], [1, 0]], dtype=int)
|
||||
result = matrix_pow_np(m, n - 1)
|
||||
return int(result[0, 0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
|
@ -242,3 +329,4 @@ if __name__ == "__main__":
|
|||
time_func(fib_memoization, num) # 0.0100 ms
|
||||
time_func(fib_recursive_cached, num) # 0.0153 ms
|
||||
time_func(fib_recursive, num) # 257.0910 ms
|
||||
time_func(fib_matrix_np, num) # 0.0000 ms
|
||||
|
|
Loading…
Reference in New Issue
Block a user