Leetcode题解(Week 7):322. Coin Change

原题目

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.

中文大意:

给定一个数组,数组中的元素,表示现有的硬币的面值,比如[1,2,5]表示有无穷多的1,2,5面值的硬币,那么再给定一张纸币,面值为x,求能将x换成以上面值的硬币的最小个数,如果无法完整地兑换,返回-1

题解:

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> dp(amount+1,0);
        for(int i =1 ; i<=amount ; i++)
        {
            int currmin = amount+1;
            for(int j = 0;j<coins.size();j++)
            {
                if(i-coins[j]>=0)
                    currmin = min(currmin,dp[i-coins[j]]+1);
            }
            dp[i] = currmin;
        }
        if(dp[amount]>amount) return -1;
        return dp[amount];
    }
};

思路:

这道题一眼看上去,好像可以通过贪心算法来解决,因为是需要换最少的硬币,那么我理论上只需要在每一次兑换中换可以对换中的面值最大的即可。但实际上这道题通过贪心算法没有办法解决。举例:当纸币为9,而硬币为[5 3 2]的时候,用贪心算法,无解。但实际上可以换(5,2,2)。于是,这道题需要通过动态规划来解决:
假设硬币面值序列为c[0,1,2…i…n],纸币面值为amount
- 每一个状态定义为f[i]:表示面值为i的纸币最少可以换成多少个硬币
- f[0] = 0
- f[i] = min{f[i-c[j]]+1} ,i-c[j]必须大于等于0,若找不到i-c[j]小于等于0 的话,f[i] = amount+1表示这个纸币不能成功地兑换
- 最后结果是f[amount](如果f[amout]<=amount)或-1(如果f[amount] > amount)

复杂度分析

显然这个问题的算法复杂度为O(n^m)空间复杂度为O(n),其中n等于纸币的面值amount,m表示硬币的种类数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值