Skip to content

Commit

Permalink
feat: coin change code committed in c++
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelina-bhatt committed Oct 28, 2024
1 parent 06463bd commit e6b5884
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cpp/coin-change.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int coinChange(const vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;

for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] = min(dp[i], dp[i - coin] + 1);
}
}

return dp[amount] > amount ? -1 : dp[amount];
}

int main() {
vector<int> coins = {1, 2, 5};
int amount = 11;
int result = coinChange(coins, amount);

if (result != -1)
cout << "Minimum coins required: " << result << endl;
else
cout << "Not possible to make the amount with the given coins." << endl;

return 0;
}

0 comments on commit e6b5884

Please sign in to comment.