-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path322 Coin Change.py
73 lines (59 loc) · 1.96 KB
/
322 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
You are given coins of different denominations and a total amount of money amount. Write a function to compute the
fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any
combination of the coins, return -1.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
Note:
You may assume that you have an infinite number of each kind of coin.
Author: Rajeev Ranjan
"""
import sys
class Solution(object):
def coinChange(self, coins, amount):
"""
DP with early prune
Let F[i] be the fewest number of coins make to i
F[i+k] = min(F[i]+1, \forall k if F[i])
O(NM)
:type coins: List[int]
:type amount: int
:rtype: int
"""
if amount == 0:
return 0
F = [sys.maxint for _ in xrange(amount+1)]
for k in coins:
if k < amount+1:
F[k] = 1
for i in xrange(1, amount+1):
if F[i] != sys.maxint:
for k in coins:
if i+k <= amount:
F[i+k] = min(F[i+k], F[i]+1)
return F[amount] if F[amount] != sys.maxint else -1
class SolutionTLE(object):
def coinChange(self, coins, amount):
"""
Let F[i] be the fewest number of coins make to i
F[i] = min(F[i-k]+1, \forall k)
O(NM)
:type coins: List[int]
:type amount: int
:rtype: int
"""
F = [sys.maxint for _ in xrange(amount+1)]
for k in coins:
if k < amount + 1:
F[k] = 1
for i in xrange(1, amount+1):
for k in coins:
if i-k > 0 and F[i-k] != sys.maxint:
F[i] = min(F[i], F[i-k]+1)
return F[amount] if F[amount] != sys.maxint else -1
if __name__ == "__main__":
assert Solution().coinChange([243, 291, 335, 209, 177, 345, 114, 91, 313, 331], 7367) == 23