class Solution {
public:
void changeHelper(vector<int> & coins, long long curr, int amount, long long count, vector<long long> & res){
if (curr > amount)
return;
else if (curr == amount){
res.push_back(count);
return;
}
else{
for (auto c: coins){
// choose
changeHelper(coins, curr+c, amount, count+1, res);
}
}
}
int coinChange(vector<int>& coins, int amount) {
// initialize helper variables
long long curr = 0;
long long count = 0;
vector<long long> res;
changeHelper(coins, curr, amount, count, res);
if (res.empty()) return -1;
int mincount = res.front();
for (auto r: res){
if (r < mincount) mincount = r;
}
return mincount;
}
};
If overflow... Then this will gets into loop many times. How to solve that?
Try to solve overflow but used long long and has memory limit exceeded error.
Try to solve overflow but used long long and has memory limit exceeded error.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.