forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoinChange.swift
34 lines (30 loc) · 999 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
34
/**
* 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 {
// edge case
guard amount != 0 else {
return 0
}
var minNums = [Int](count: amount + 1, repeatedValue: -1)
for i in 0 ... amount {
for coin in coins {
if coin == i {
minNums[i] = 1
break
}
if coin < i && minNums[i - coin] != -1 {
if minNums[i] == -1 {
minNums[i] = minNums[i - coin] + 1
} else {
minNums[i] = min(minNums[i], minNums[i - coin] + 1)
}
}
}
}
return minNums[amount]
}
}