|
| 1 | +/* |
| 2 | + * @file CoinChange.h |
| 3 | + * @author (original JAVA) William Fiset, [email protected] |
| 4 | + * (conversion to C++) Armin Zare Zadeh, [email protected] |
| 5 | + * @date 15 July 2020 |
| 6 | + * @version 0.1 |
| 7 | + * @brief The coin change problem is an unbounded knapsack problem variant. The problem asks you to find |
| 8 | + * the minimum number of coins required for a certain amount of change given the coin denominations. |
| 9 | + * You may use each coin denomination as many times as you please. |
| 10 | + * |
| 11 | + * <p>Tested against: https://leetcode.com/problems/coin-change/ |
| 12 | + */ |
| 13 | + |
| 14 | +#ifndef D_COINCHANGE_H |
| 15 | +#define D_COINCHANGE_H |
| 16 | + |
| 17 | +#include <vector> |
| 18 | +#include <deque> |
| 19 | +#include <list> |
| 20 | +#include <set> // set and multiset |
| 21 | +#include <map> // map and multimap |
| 22 | +#include <unordered_set> // unordered set/multiset |
| 23 | +#include <unordered_map> // unordered map/multimap |
| 24 | +#include <iterator> |
| 25 | +#include <algorithm> |
| 26 | +#include <numeric> // some numeric algorithm |
| 27 | +#include <functional> |
| 28 | +#include <stack> |
| 29 | + |
| 30 | +#include <sstream> |
| 31 | +#include <memory> |
| 32 | +#include <iostream> |
| 33 | +#include <cmath> |
| 34 | + |
| 35 | +namespace dsa { |
| 36 | + |
| 37 | +class CoinChange { |
| 38 | + |
| 39 | +private: |
| 40 | + static const int INF; |
| 41 | +public: |
| 42 | + static int coinChange(const std::vector<int>& coins, int amount) { |
| 43 | + |
| 44 | + if (coins.size() == 0) throw std::invalid_argument("No coin values :/"); |
| 45 | + |
| 46 | + int N = coins.size(); |
| 47 | + // Initialize table and set first row to be infinity |
| 48 | + std::vector<std::vector<int>> DP(N + 1, std::vector<int>(amount + 1, INF)); |
| 49 | + DP[1][0] = 0; |
| 50 | + |
| 51 | + // Iterate through all the coins |
| 52 | + for (int i = 1; i <= N; i++) { |
| 53 | + |
| 54 | + int coinValue = coins[i - 1]; |
| 55 | + for (int j = 1; j <= amount; j++) { |
| 56 | + |
| 57 | + // Consider not selecting this coin |
| 58 | + DP[i][j] = DP[i - 1][j]; |
| 59 | + |
| 60 | + // Try selecting this coin if it's better |
| 61 | + if (j - coinValue >= 0 && DP[i][j - coinValue] + 1 < DP[i][j]) |
| 62 | + DP[i][j] = DP[i][j - coinValue] + 1; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // The amount we wanted to make cannot be made :/ |
| 67 | + if (DP[N][amount] == INF) return -1; |
| 68 | + |
| 69 | + // Return the minimum number of coins needed |
| 70 | + return DP[N][amount]; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + static int coinChangeSpaceEfficient(const std::vector<int>& coins, int amount) { |
| 75 | + |
| 76 | + if (coins.size() == 0) throw std::invalid_argument("Coins array is null"); |
| 77 | + |
| 78 | + // Initialize table and set everything to infinity except first cell |
| 79 | + std::vector<int> DP(amount + 1, INF); |
| 80 | + DP[0] = 0; |
| 81 | + |
| 82 | + for (int i = 1; i <= amount; i++) |
| 83 | + for (int coinValue : coins) |
| 84 | + if (i - coinValue >= 0 && DP[i - coinValue] + 1 < DP[i]) DP[i] = DP[i - coinValue] + 1; |
| 85 | + |
| 86 | + // The amount we wanted to make cannot be made :/ |
| 87 | + if (DP[amount] == INF) return -1; |
| 88 | + |
| 89 | + // Return the minimum number of coins needed |
| 90 | + return DP[amount]; |
| 91 | + } |
| 92 | + |
| 93 | +public: |
| 94 | + // The recursive approach has the advantage that it does not have to visit |
| 95 | + // all possible states like the tabular approach does. This can speedup |
| 96 | + // things especially if the coin denominations are large. |
| 97 | + static int coinChangeRecursive(const std::vector<int>& coins, int amount) { |
| 98 | + |
| 99 | + if (coins.size() == 0) throw std::invalid_argument("Coins array is null"); |
| 100 | + if (amount < 0) return -1; |
| 101 | + |
| 102 | + std::vector<int> DP(amount + 1); |
| 103 | + return coinChangeRecursive(amount, coins, DP); |
| 104 | + } |
| 105 | + |
| 106 | +private: |
| 107 | + // Private helper method to actually go the recursion |
| 108 | + static int coinChangeRecursive(int amount, const std::vector<int>& coins, std::vector<int>& DP) { |
| 109 | + |
| 110 | + // Base cases. |
| 111 | + if (amount < 0) return -1; |
| 112 | + if (amount == 0) return 0; |
| 113 | + if (DP[amount] != 0) return DP[amount]; |
| 114 | + |
| 115 | + int minCoins = INF; |
| 116 | + for (int coinValue : coins) { |
| 117 | + |
| 118 | + int newAmount = amount - coinValue; |
| 119 | + int value = coinChangeRecursive(newAmount, coins, DP); |
| 120 | + if (value != -1 && value < minCoins) minCoins = value + 1; |
| 121 | + } |
| 122 | + |
| 123 | + // If we weren't able to find some coins to make our |
| 124 | + // amount then cache -1 as the answer. |
| 125 | + return DP[amount] = (minCoins == INF) ? -1 : minCoins; |
| 126 | + } |
| 127 | + |
| 128 | +}; |
| 129 | +const int CoinChange::INF = 987654321; |
| 130 | + |
| 131 | + |
| 132 | +void CoinChange_test() |
| 133 | +{ |
| 134 | + const std::vector<int> coins{2, 6, 1}; |
| 135 | + std::cout << CoinChange::coinChange(coins, 17) << std::endl; |
| 136 | + std::cout << CoinChange::coinChangeSpaceEfficient(coins, 17) << std::endl; |
| 137 | + std::cout << CoinChange::coinChangeRecursive(coins, 17) << std::endl; |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace dsa |
| 141 | + |
| 142 | +#endif /* D_JOSEPHUSPROBLEM_H */ |
0 commit comments