#include<vector>
#include<algorithm>
#include<functional>
#include<iostream>
using namespace std;
class Solution {
public:
int coinChange(vector<int>& coins, int amount)
{
int fn[10001];
std::fill(fn,fn+10001,-1);
fn[0] = 0;
std::vector<int> fvalues;
for(int i=1; i<=amount; ++i)
{
for(auto coin : coins)
{
int value = i - coin;
if(value < 0) continue;
else if(fn[value]==-1) continue;
else{
fvalues.push_back(fn[value]);
}
}
if(fvalues.empty()==false)
{
fn[i] = *(std::min_element(fvalues.begin(),fvalues.end())) + 1;
}
fvalues.clear();
}
return fn[amount];
}
};
结果
Accepted
189/189 cases passed (52 ms)
Your runtime beats 93.76 % of cpp submissions
Your memory usage beats 98.06 % of cpp submissions (10.1 MB)