Question - You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount.Infinite supply of coin is there.
My Approach - I have followed the top-down approach but with memoization using map stl, I am getting TLE. Please help in finding out the error and estimating the time complexity.
Here is my code -
// for example coins v = {1,2} and W = 2 then possible solutions are -
// (2) or (1, 1), therefore minimum number of coins required is 1.
// Below is the function calculating the minimum number of coins required for change
int minCoinChange(vector<int> &v, int start, int W, unordered_map<string, int> &lookup){
// v denoting the vector conataining coins with given denomination
// start denoting the start index i.e. 0(initially)
// W denoting the required change
// lookup is map stl for storing values.
// base cases
if(W<0){
return INT_MAX;
}
if(W == 0){
return 0;
}
if(start >= v.size()){
return INT_MAX;
}
// for memoization creating the "key"
string key = to_string(start) + '|' + to_string(W);
// if the key is not found in map then go inside if
if(lookup.find(key) == lookup.end()){
int excl = minCoinChange(v, start+1, W, lookup); // if element is excluded
int incl = 1 + minCoinChange(v, start, W - v[start], lookup); if element is included
lookup[key] = min(incl, excl); // calculating minimum number of coins required and storing in map
}
// if key is already there then return value stored in map
return lookup[key];
}