-
Notifications
You must be signed in to change notification settings - Fork 0
/
322-Coin-Change.py
87 lines (69 loc) · 2.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Solution:
def coinChange(self, coins, amount):
value_list = [amount+1] * (amount+1)
value_list[0] = 0
for i in range( 1, amount+1 ):
for coin in coins:
if i >= coin:
value_list[i] = min( value_list[i-coin] + 1, value_list[i] )
return -1 if value_list[amount] == amount + 1 else value_list[amount]
# solution II
class SolutionII:
def coinChange(self, coins: List[int], amount: int) -> int:
MAX = float( 'inf' ) #infinite large
coin_num = [0] + [MAX]*amount
for i in range(1,amount+1):
coin_num[i] = min( [ coin_num[i - c] if i - c >=0 else MAX for c in coins ] ) + 1
return [coin_num[amount], -1][coin_num[amount]==MAX]
# solution with BFS
class SolutionIII:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
visited = [False]*(amount+1)
visited[0] = True
coin_num = 0
value_now, value_next = [0], []
while value_now:
coin_num += 1
for value in value_now:
for coin in coins:
newvalue = value + coin
if newvalue == amount:
return coin_num
elif newvalue > amount:
continue
elif not visited[newvalue]:
visited[newvalue] = True
value_next.append( newvalue )
value_now, value_next = value_next, []
return -1
# solution with DFS
class SolutionIIV:
def coinChange(self, coins, amount):
coins.sort( reverse = True)
lenc, self.min_step = len(coins), float('inf')
def dfs( point, amo , count ):
if not amo: #amount != 0
self.min_step = min( self.min_step, count )
for i in range( point, lenc ):
if coins[i] <= amo < coins[i] * (self.min_step - count):
dfs( i, amo-coins[i], count+1 )
for i in range( lenc ):
dfs( i, amount, 0 )
return [self.min_step, -1][ self.min_step == float('inf') ]
# solution with DFS, interative
class Solution:
def coinChange(self, coins, amount):
coins.sort()
lenc, min_steps = len(coins), float('inf')
dfs_stack = [ (0, 0, lenc) ]
while len(dfs_stack) != 0:
steps, value_now, coins_num = dfs_stack.pop()
if value_now == amount:
min_steps = min( min_steps, steps )
if value_now > amount or amount - value_now > coins[ coins_num-1 ] * (min_steps - steps):
continue
for coinnum, coin in enumerate( coins[:coins_num] ):
dfs_stack.append( (steps+1, value_now + coin, coinnum + 1) )
return min_steps if min_steps != float('inf') else -1