Professional Documents
Culture Documents
167 AMAANKHANPATHAN Assignemt 4 DAA PDF
167 AMAANKHANPATHAN Assignemt 4 DAA PDF
This problem can be solved using dynamic programming. We can define a table
where the rows represent the different denominations of coins, and the columns
represent the target values. We can then fill in the table row by row, computing the
minimum number of coins required for each value, using the values in the previous
row.
The base case is when the target value is 0, in which case we require 0 coins for any
denomination. For each subsequent value, we consider all possible coin
denominations and choose the one that results in the minimum number of coins.
We can express this recursively as follows:
Once we have computed the table, we can read off the minimum number of coins
required for the target value by looking up the value in the table for the highest
denomination coin.
The time complexity :-
The time complexity of the coin changing problem using dynamic programming is
O(nm), where n is the number of coin denominations and m is the target value.
This is because we need to fill in a table of size n x m, and for each cell in the
table, we consider all possible coin denominations, which takes O(n) time.
Therefore, the total time complexity is O(nm).
Code:
// A Naive recursive C++ program to find minimum of coins
// to make a given change V
#include <algorithm>
#include <chrono>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
// Initialize result
int res = INT_MAX;
int m = sizeof(coins)/sizeof(coins[0]);
int V ;
cout<<endl<<"Enter Total sum: ";
cin>>V;
auto start = high_resolution_clock::now();
cout << "Minimum coins required is: "<< minCoins(coins, m, V);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
return 0;
}
Output:-