-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0322_coin_change.py
More file actions
106 lines (82 loc) · 2.82 KB
/
0322_coin_change.py
File metadata and controls
106 lines (82 loc) · 2.82 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# ------------------------------------------------------------------------------
# Question:
# ------------------------------------------------------------------------------
# tags:
'''
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:
Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Note:
You may assume that you have an infinite number of each kind of coin.
'''
# ------------------------------------------------------------------------------
# Solutions
# ------------------------------------------------------------------------------
import unittest
from typing import *
class Solution:
'''
Time:
Space:
DFS
'''
def coinChange(self, coins, amount):
def recur(target, count):
if target == 0:
return count
if target < 0:
return float("inf")
# result = []
# for c in coin:
# result.append(recur(coin, amount-c, count+1))
# return min(result)
return min([recur(target - c, count + 1) for c in coins])
result = recur(amount, 0)
return result if result != float("inf") else -1
class SolutionGreedy:
'''
Time: O(n+1)
Space: O(n)
Intuition: dp[i] stores minimum number of coins for target amount i
'''
def coinChange(self, coins, amount):
dp = [float("inf")] * (amount + 1)
dp[0] = 0
for i in range(len(dp)):
print(dp)
if dp[i] != float("inf"):
for c in coins:
if i + c <= amount:
dp[i + c] = min(dp[i + c], dp[i] + 1)
return dp[-1] if dp[-1] != float("inf") else -1
class SolutionDP2:
def coinChange(self, coins, amount):
dp = [float("inf")] * (amount + 1)
dp[0] = 0
for i in range(1, len(dp)):
for c in coins:
if i - c >= 0:
dp[i] = min(dp[i], dp[i - c] + 1)
return dp[-1] if dp[-1] != float("inf") else -1
# ------------------------------------------------------------------------------
# Tests
# ------------------------------------------------------------------------------
class TestSolution(unittest.TestCase):
def test_simple(self):
coins = [1, 2, 5]
amount = 11
s = Solution()
self.assertEqual(s.coinChange(coins, amount), 3)
s = SolutionGreedy()
self.assertEqual(s.coinChange(coins, amount), 3)
s = SolutionDP2()
self.assertEqual(s.coinChange(coins, amount), 3)
unittest.main(verbosity=2)