LeetCode:求最少的硬币个数组成指定的金额

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 make up that 
amount.If that amount of money cannot be made up by any combination of the coins,
return -1.

Example 1:
Input:coins=[1,2,5],amount=11
Ouput:3(11=5+5+1)

Example 2:
Input:coins=[2],amount=3
Output:-1

Note:
You may assume that you have an infinite number of each kind of coin.

题目大意:
给定一些硬币的面额,求最少可以用多少颗硬币组成给定的金额.

解题思路:
因为每个硬币可以使用无限次数,这道题本质上是完全背包的问题.可以直接展示二维空间压缩为一维的写法.
这里把dp数组初始化为amount+2而不是-1的原因是,在动态规划过程中有求最小值的操作,如果初始化成-1
则会导致结果始终为-1.至于为什么取这个值,是因为i的最大取值为amount+1,而最多的组成方式是只用
1元硬币,因此amount+2一定大于所有的可能的组合方式.在dp后,若结果仍为此值,则说明不存在满足添加的组合
方法,返回-1.
#include <iostream>
#include <vector>

using namespace std;

class Solution{
public:
    int coinChange(vector<int>& coins,int amount){
        if(coins.empty())
           return -1;
        vector<int> dp(amount+1,amount+2);
        dp[0]=0;
        for(auto i=1;i<=amount;i++)
            for(const int& coin:coins){
                if(i>=coin){
                    dp[i]=min(dp[i],dp[i-coin]+1);
                }  
            }
        return dp[amount]==amount+2?-1:dp[amount];
    }
};

int main(int argc,char** argv){
    vector<int> coins={1,2,5};
    cout<<Solution().coinChange(coins,11)<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值