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:

Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

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

题目大意:

使用几种硬币找零钱,每种硬币可多次使用,求所需硬币最少数目。若硬币之和不能等于零钱总数,输出-1.

解题思路:

用递归的动态规划解决

 class Solution {
public: const int inf = ;
vector<vector<int>> v;
int search(vector<int>& coins, int amount, int idx) {
if (amount == )
return ;
if (idx >= coins.size() || amount < )
return inf;
if (v[idx][amount] >= )
return v[idx][amount];
int a = search(coins, amount - coins[idx], idx);
int b = search(coins, amount, idx + );
v[idx][amount] = min(a + , b);
return v[idx][amount];
} int coinChange(vector<int>& coins, int amount) {
v.resize(coins.size(), vector<int>(amount + , -));
int ans = search(coins, amount, );
if (ans < inf)
return ans;
return -;
}
};

方法二:迭代

定义一个大小为amount + 1的数组dp,dp[i]代表当总钱币数为i时可兑换的最少的钱币个数,

当i为0时只需要0个钱币,因此dp[0]等于0。

当求dp[k]时,先令dp[k]等于正无穷,然后遍历所有钱币,若钱币j币值coins[j]小于k,令dp[k] = min(dp[k - coins[j] ] + 1, dp[k])。

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

leetcode 322零钱兑换的更多相关文章

  1. Java实现 LeetCode 322 零钱兑换

    322. 零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输 ...

  2. Leetcode 322.零钱兑换

    零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...

  3. [LeetCode]322. 零钱兑换(DP)

    题目 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coin ...

  4. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

  5. Leetcode 518.零钱兑换II

    零钱兑换II 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 注意: 你可以假设 0 <= amount (总金额) <= 500 ...

  6. Java实现 LeetCode 518 零钱兑换 II

    518. 零钱兑换 II 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 示例 1: 输入: amount = 5, coins = [1, ...

  7. LeetCode:322. 零钱兑换

    链接:https://leetcode-cn.com/problems/coin-change/ 标签:动态规划.完全背包问题.广度优先搜索 题目 给定不同面额的硬币 coins 和一个总金额 amo ...

  8. Leetcode题目322.零钱兑换(动态规划-中等)

    题目描述: 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: c ...

  9. [Leetcode][动态规划] 零钱兑换

    一.题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: ...

随机推荐

  1. [转] 理解SVG transform坐标变换

    http://www.zhangxinxu.com/wordpress/2015/10/understand-svg-transform/

  2. PIE SDK与Matlab结合说明文档

    1.功能简介 Matlab是三大数学软件之一,它在数学类科技应用软件中在数值计算方面首屈一指.Matlab可以进行矩阵运算.绘制函数和数据.实现算法.创建用户界面.连接其他编程语言的程序等,主要应用于 ...

  3. windows下dubbo-admin的安装

    本来以为十分钟就能搞定的东西结果搞了一个小时,也是菜到抠脚,赶紧记录一下. 下载dubbo源码,下载地址:https://download.csdn.net/download/huangzhang_/ ...

  4. 转载收藏(js数组方法大全)

    js数组方法大全 JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组var arr2 = new Arra ...

  5. git 拉新项目

                   

  6. linux拓展之 用linux命令 管理windows一秒完成不可思议的操作--本节实战find 移动!!

    花里胡哨的东西太多,有时候觉得简单也好! 你学习了Linux,是不是觉得Linux很强大!命令的多样性结合性有没有把你征服? 在那个烈日炎炎的夏日,我下载了辣末多老男孩的视屏----但是突然我只想看t ...

  7. HTML问题 | 两个Input在同一行连着不留缝隙

    方法1:让两个 input 连在一起写 不换行 <div class="inputDiv"> <input type="text" place ...

  8. Linux 上安装 weblogic12C (静默安装) (一)

    最近负责在linux上安装weblogic,客户说要安装最新的版本,版本号为 12.1.X(12.1.2,12.1.3).开始以为和旧版安装一样,使用控制台的方式,下载bin文件,然后一步步在cons ...

  9. TOJ 1023 Taxi Cab Scheme

    Description Running a taxi station is not all that simple. Apart from the obvious demand for a centr ...

  10. 浅谈前端与SEO

    转载地址: https://blog.csdn.net/lzm18064126848/article/details/53385274?tdsourcetag=s_pctim_aiomsg SEO(S ...