TheAlgorithms-C-Plus-Plus/dynamic_programming/Coin-Change.cpp

50 lines
1.0 KiB
C++
Raw Normal View History

2019-07-31 01:10:44 +08:00
#include <iostream>
2019-08-21 10:10:08 +08:00
#include <climits>
2019-07-31 01:10:44 +08:00
using namespace std;
// Function to find the Minimum number of coins required to get Sum S
int findMinCoins(int arr[], int n, int N)
{
// dp[i] = no of coins required to get a total of i
int dp[N + 1];
2019-08-21 10:10:08 +08:00
2019-07-31 01:10:44 +08:00
// 0 coins are needed for 0 sum
2019-08-21 10:10:08 +08:00
dp[0] = 0;
2019-07-31 01:10:44 +08:00
for (int i = 1; i <= N; i++)
{
// initialize minimum number of coins needed to infinity
dp[i] = INT_MAX;
int res = INT_MAX;
// do for each coin
for (int c = 0; c < n; c++)
{
if (i - arr[c] >= 0) // check if coins doesn't become negative by including it
res = dp[i - arr[c]];
// if total can be reached by including current coin c,
// update minimum number of coins needed dp[i]
if (res != INT_MAX)
dp[i] = min(dp[i], res + 1);
}
}
// The Minimum No of Coins Required for N = dp[N]
return dp[N];
}
int main()
{
// No of Coins We Have
2019-08-21 10:10:08 +08:00
int arr[] = {1, 2, 3, 4};
2019-07-31 01:10:44 +08:00
int n = sizeof(arr) / sizeof(arr[0]);
// Total Change Required
int N = 15;
2019-08-21 10:10:08 +08:00
cout << "Minimum Number of Coins Required " << findMinCoins(arr, n, N) << "\n";
2019-07-31 01:10:44 +08:00
return 0;
}