#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
/*
问题:
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.
If that amount of money cannot be made up by any combination of the coins, return -1.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
Note:
You may assume that you have an infinite number of each kind of coin.
分析:这明显是给定硬币数组,判断给定的钱能否用最少的硬币兑换。
这个是回溯。但是回溯需要优先从硬币值高到低进行回溯。
边界情况:如果给定的价格=0,此题没有意义
输入:
3(硬币个数) 11(amount)
1 2 5
1 3
2
4 39
1 2 4 10
3 14
2 7 10
1 0
输出:
3
-1
6
2
0
关键:
1 超时
Your input:[1] 0
Your answer
Expected answer:0
也就是说如果传入的待兑换的钱为0,则无需兑换,直接返回0
2超时
Last executed input:
[77,82,84,80,398,286,40,136,162] 9794
难道是递归中重复计算,可以把一些如果之前已经计算过的直接返回
参考leecode解法:https://leetcode.com/problems/coin-change/?tab=Solutions
用动态规划来做。设dp[i]表示钱i最少需要的硬币兑换次数,
那么钱i的最少硬币兑换次数要么来源于其本身比如一次就可以直接兑换,要么来源于
通过钱i - coins[i]的最少兑换次数+1
dp[i] = min( dp[i] , dp[i - coins[j] ] + 1
为了能够计算dp[i - coins[j]],我们先遍历一遍值面值i在1到amount,尝试计算出
dp。
//并且面值i-coins[j]兑换次数也需要能够兑换
if(i >= coins.at(j) && dp.at(i - coins.at(j)) != INT_MAX)
{
*/
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(coins.empty() || amount < 0)
{
return -1;
}
//易错如果待兑换的面值为0,无需兑换,直接返回0
if(0 == amount)
{
return 0;
}
vector<int> dp(amount + 1 , INT_MAX);//初始化为一个不可能的值
dp.at(0) = 0;//面值0无需硬币兑换
int size = coins.size();
for(int i = 1 ; i <= amount ; i++)
{
for(int j = 0 ; j < size ; j++)
{
//并且面值i-coins[j]兑换次数也需要能够兑换
if(i >= coins.at(j) && dp.at(i - coins.at(j)) != INT_MAX)
{
//当前面值i需要的最少兑换次数要么就是之前保留的最少兑换次数
//要么就是在面值i-coins[j]的最少兑换次数上加1
dp.at(i) = min(dp.at(i) , dp.at( i - coins.at(j) ) + 1);
}
}
}
if(dp.at(amount) != INT_MAX)
{
return dp.at(amount);
}
//无法兑换
else
{
return -1;
}
}
};
void process()
{
vector<int> coins;
int value;
int num;
Solution solution;
int result;
int amount;
while(cin >> num >> amount)
{
coins.clear();
for(int i = 0 ; i < num ; i++)
{
cin >> value;
coins.push_back(value);
}
result = solution.coinChange(coins , amount);
cout << result << endl;
}
}
int main(int argc , char* argv[])
{
process();
getchar();
return 0;
}
leecode 解题总结:322. Coin Change
最新推荐文章于 2024-08-12 21:02:24 发布
