-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_change.py
35 lines (28 loc) · 1.03 KB
/
coin_change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from typing import List
def coinChange(coins: List[int], amount: int) -> int:
dp = [amount + 1] * (amount + 1)
dp[0] = 0
for a in range(1, amount + 1):
for c in coins:
if c <= a:
dp[a] = min(dp[a], dp[a - c] + 1)
return -1 if dp[amount] > amount else dp[amount]
"""
Algorithm is based on the following recurrence relation:
dp[a] = min(dp[a], dp[a - c] + 1)
where:
a is the amount of money
c is the coin value
dp[a] is the minimum number of coins needed to make up the amount a
dp[a - c] is the minimum number of coins needed to make up the amount a - c
+ 1 is the coin we are adding to the solution
The algorithm is based on the following observations:
The minimum number of coins needed to make up the amount a is the minimum
number of coins needed to make up the amount a - c plus one coin.
Analysis:
Time complexity: O(a * c) = o(n^2)
Space complexity: O(a) = O(n)
"""
assert coinChange([1,2,5], 11) == 3
assert coinChange([2], 3) == -1
assert coinChange([1], 0) == 0