Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cpp/322-Coin-Change.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1); // minimum coin combinations for 0 to amount, set default max value as amount+1(which is impossible to make)
dp[0] = 0; // base case: if amount is 0, 0 coins
for (int a = 1; a <= amount; ++a) {
for (int c : coins) {
if (a - c >= 0) // possible to try
// min of (current min combination, using one for this coin and min of remainder)
dp[a] = min(dp[a], 1 + dp[a - c]);
}
}
return dp[amount] != amount + 1 ? dp[amount] : -1;
}
};