leetcode 322零钱兑换
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零钱兑换的更多相关文章
- Java实现 LeetCode 322 零钱兑换
322. 零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输 ...
- Leetcode 322.零钱兑换
零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...
- [LeetCode]322. 零钱兑换(DP)
题目 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coin ...
- LeetCode:零钱兑换【322】【DP】
LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...
- Leetcode 518.零钱兑换II
零钱兑换II 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 注意: 你可以假设 0 <= amount (总金额) <= 500 ...
- Java实现 LeetCode 518 零钱兑换 II
518. 零钱兑换 II 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 示例 1: 输入: amount = 5, coins = [1, ...
- LeetCode:322. 零钱兑换
链接:https://leetcode-cn.com/problems/coin-change/ 标签:动态规划.完全背包问题.广度优先搜索 题目 给定不同面额的硬币 coins 和一个总金额 amo ...
- Leetcode题目322.零钱兑换(动态规划-中等)
题目描述: 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: c ...
- [Leetcode][动态规划] 零钱兑换
一.题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: ...
随机推荐
- [转] 用Python建立最简单的web服务器
[From] http://www.cnblogs.com/xuxn/archive/2011/02/14/build-simple-web-server-with-python.html 利用Pyt ...
- hdu2588 GCD
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- vue 浏览器顶部有载入(进度)动画插件vue-progressbar
1.安装 npm install --save nprogress 2.在main.js中引入 import NProgress from "nprogress" import & ...
- spark第四篇:Running Spark on YARN
确保HADOOP_CONF_DIR或者YARN_CONF_DIR指向hadoop集群配置文件目录.这些配置用来写数据到hdfs以及连接yarn ResourceManager.(在$SPARK_HOM ...
- golang中的文件操作
一.文件的基本介绍 文件是数据源(保存数据的地方)的一种,比如经常使用的word文档,txt文件,excel文件都是文件.文件最主要的作用就是保存数据,它既可以保存一张图片,也可以保持视频,声音等等. ...
- maven 引入本地 jar
$ 参考1 : https://www.cnblogs.com/lixuwu/p/5855031.html ! 注: 参考1中的第二种方法,作者并未实际尝试,我尝试了,虽然在eclipse 中编译不报 ...
- (转) CentOS 7添加开机启动服务/脚本
CentOS 7添加开机启动服务/脚本 原文:http://blog.csdn.net/wang123459/article/details/79063703 一.添加开机自启服务 在CentOS 7 ...
- Js简易代码生成工具
代码 javascript:(function(){ document.body.innerHTML = '<textarea id="txtTemplate" style= ...
- CSS布局——左定宽度右自适应宽度并且等高布局
方法一: 别的不多说,直接上代码,或者参考在线DEMO,下面所有的DEMO都有HTML和CSS代码,感兴趣的同学自己慢慢看吧. HTML Markup <div id="contain ...
- centos系统为php安装memcached扩展
1. 通过yum安装 yum -y install memcached #安装完成后执行: memcached -h #出现memcached帮助信息说明安装成功 2. 加入启动服务 chkconfi ...