零钱兑换
给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 -1 。你可以认为每种硬币的数量是无限的。
class Solution
{
public:
int coinChange(vector<int>& coins, int amount)
{
vector<int> res(amount + 1, amount + 1);
res[0] = 0;
int nSize = coins.size();
for (int i = 1; i <= amount; ++i)
{
for (int j = 0; j < nSize; ++j)
{
if (coins[j] <= i)
{
res[i] = min(res[i], res[i - coins[j]] + 1);
}
}
}
return res[amount] > amount ? -1 : res[amount];
}
};