|
1 | 1 | //
|
2 |
| -// Minimum Coin Change Problem |
| 2 | +// Minimum Coin Change Problem Playground |
3 | 3 | // Compare Greedy Algorithm and Dynamic Programming Algorithm in Swift
|
4 | 4 | //
|
5 | 5 | // Created by Jacopo Mangiavacchi on 04/03/17.
|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Foundation
|
9 | 9 |
|
| 10 | +public enum MinimumCoinChangeError: Error { |
| 11 | + case noRestPossibleForTheGivenValue |
| 12 | +} |
| 13 | + |
10 | 14 | public struct MinimumCoinChange {
|
11 |
| - private let sortedCoinSet: [Int] |
12 |
| - private var cache: [Int : [Int]] |
| 15 | + internal let sortedCoinSet: [Int] |
13 | 16 |
|
14 | 17 | public init(coinSet: [Int]) {
|
15 | 18 | self.sortedCoinSet = coinSet.sorted(by: { $0 > $1} )
|
16 |
| - self.cache = [:] |
17 | 19 | }
|
18 | 20 |
|
19 | 21 | //Greedy Algorithm
|
20 |
| - public func changeGreedy(_ value: Int) -> [Int] { |
| 22 | + public func changeGreedy(_ value: Int) throws -> [Int] { |
| 23 | + guard value > 0 else { return [] } |
| 24 | + |
21 | 25 | var change: [Int] = []
|
22 | 26 | var newValue = value
|
23 |
| - |
| 27 | + |
24 | 28 | for coin in sortedCoinSet {
|
25 | 29 | while newValue - coin >= 0 {
|
26 | 30 | change.append(coin)
|
27 | 31 | newValue -= coin
|
28 | 32 | }
|
29 | 33 |
|
30 |
| - if newValue == 0 { |
| 34 | + if newValue == 0 { |
31 | 35 | break
|
32 | 36 | }
|
33 | 37 | }
|
34 | 38 |
|
| 39 | + if newValue > 0 { |
| 40 | + throw MinimumCoinChangeError.noRestPossibleForTheGivenValue |
| 41 | + } |
| 42 | + |
35 | 43 | return change
|
36 | 44 | }
|
37 | 45 |
|
38 | 46 | //Dynamic Programming Algorithm
|
39 |
| - public mutating func changeDynamic(_ value: Int) -> [Int] { |
40 |
| - if value <= 0 { |
41 |
| - return [] |
42 |
| - } |
| 47 | + public func changeDynamic(_ value: Int) throws -> [Int] { |
| 48 | + guard value > 0 else { return [] } |
43 | 49 |
|
44 |
| - if let cached = cache[value] { |
45 |
| - return cached |
46 |
| - } |
47 |
| - |
48 |
| - var change: [Int] = [] |
49 |
| - |
50 |
| - var potentialChangeArray: [[Int]] = [] |
51 |
| - |
52 |
| - for coin in sortedCoinSet { |
53 |
| - if value - coin >= 0 { |
54 |
| - var potentialChange: [Int] = [] |
55 |
| - potentialChange.append(coin) |
56 |
| - let newPotentialValue = value - coin |
| 50 | + var cache: [Int : [Int]] = [:] |
57 | 51 |
|
58 |
| - if value > 0 { |
59 |
| - potentialChange.append(contentsOf: changeDynamic(newPotentialValue)) |
60 |
| - } |
| 52 | + func _changeDynamic(_ value: Int) -> [Int] { |
| 53 | + guard value > 0 else { return [] } |
61 | 54 |
|
62 |
| - //print("value: \(value) coin: \(coin) potentialChange: \(potentialChange)") |
63 |
| - potentialChangeArray.append(potentialChange) |
| 55 | + if let cached = cache[value] { |
| 56 | + return cached |
64 | 57 | }
|
| 58 | + |
| 59 | + var change: [Int] = [] |
| 60 | + |
| 61 | + var potentialChangeArray: [[Int]] = [] |
| 62 | + |
| 63 | + for coin in sortedCoinSet { |
| 64 | + if value - coin >= 0 { |
| 65 | + var potentialChange: [Int] = [coin] |
| 66 | + potentialChange.append(contentsOf: _changeDynamic(value - coin)) |
| 67 | + potentialChangeArray.append(potentialChange) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if potentialChangeArray.count > 0 { |
| 72 | + let sortedPotentialChangeArray = potentialChangeArray.sorted(by: { $0.count < $1.count }) |
| 73 | + change = sortedPotentialChangeArray[0] |
| 74 | + } |
| 75 | + |
| 76 | + if change.reduce(0, +) == value { |
| 77 | + cache[value] = change |
| 78 | + } |
| 79 | + |
| 80 | + return change |
65 | 81 | }
|
66 |
| - |
67 |
| - if potentialChangeArray.count > 0 { |
68 |
| - let sortedPotentialChangeArray = potentialChangeArray.sorted(by: { $0.count < $1.count }) |
69 |
| - change = sortedPotentialChangeArray[0] |
70 |
| - } |
71 |
| - |
72 |
| - cache[value] = change |
| 82 | + |
| 83 | + let change: [Int] = _changeDynamic(value) |
| 84 | + |
| 85 | + if change.reduce(0, +) != value { |
| 86 | + throw MinimumCoinChangeError.noRestPossibleForTheGivenValue |
| 87 | + } |
| 88 | + |
73 | 89 | return change
|
74 | 90 | }
|
75 | 91 | }
|
76 |
| - |
|
0 commit comments