-
Notifications
You must be signed in to change notification settings - Fork 889
/
CoinChange.swift
33 lines (27 loc) · 963 Bytes
/
CoinChange.swift
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
/**
* Question Link: https://leetcode.com/problems/coin-change/
* Primary idea: Dynamic Programming, transition function is min[i] = min[i - coin] + 1
* Time Complexity: O(n^2), Space Complexity: O(n)
*/
class CoinChange {
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
guard amount > 0 else {
return 0
}
let coins = coins.sorted()
var minAmounts = Array(repeating: -1, count: amount + 1)
minAmounts[0] = 0
for i in 1...amount {
for coin in coins {
if coin > i {
break
}
if minAmounts[i - coin] == -1 {
continue
}
minAmounts[i] = minAmounts[i] == -1 ? minAmounts[i - coin] + 1 : min(minAmounts[i - coin] + 1, minAmounts[i])
}
}
return minAmounts[amount]
}
}