{LeetCode} 518. CoinChange2
问题:
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
Note: You can assume that
- 0 <= amount <= 5000
- 1 <= coin <= 5000
- the number of coins is less than 500
- the answer is guaranteed to fit into signed 32-bit integer
我的代码:
class Solution {
public:int change(int amount, vector<int>& coins) {int count = 0;if(amount==0) return 1;else if(coins.empty()) return 0;for(int i=0; i<coins.size();i++){if(amount==coins.at(i)){count++;}else if(amount>coins.at(i)){vector<int> temp(coins);vector<int>::iterator it;for(it=temp.begin();it!=temp.end();){if(*it>coins.at(i)){it = temp.erase(it);}else{it++;}}count+=change(amount-coins.at(i),temp);}}return count;}
};
结果:
Submission Result: Time Limit Exceeded
时间复杂度太高了,改用dynamic programming:
class Solution {
public:int change(int amount, vector<int>& coins) {vector <vector<int> > dp(coins.size()+1, vector<int>(amount+1));dp.at(0).at(0)=1;for(int i=1;i<=coins.size();i++){dp.at(i).at(0)=1;for(int j=1;j<=amount;j++){dp.at(i).at(j) = dp.at(i-1).at(j) + ((j-coins.at(i-1))>=0? dp.at(i).at(j-coins.at(i-1)):0); }}return dp.at(coins.size()).at(amount);}
};
{LeetCode} 518. CoinChange2相关推荐
- LeetCode 518 和LeetCode 377 的比较
这是之前问题的变种(前3题用回溯法,最后一题用DP) Leetcode 39:组合总和(最详细的解法!!!) Leetcode 40:组合总和 II(最详细的解法!!!) Leetcode 216:组 ...
- LeetCode 518 Coin Change 2 (python)
目录 LeetCode 518 Coin Change 2 (python) Code 欢迎一起来参与leetcode刷题项目 LeetCode 518 Coin Change 2 (python) ...
- leetcode:518. 零钱兑换 II
518 零钱兑换 II 来源:力扣(LeetCode) 链接: https://leetcode.cn/problems/coin-change-2/ 给你一个整数数组 coins 表示不同面额的硬币 ...
- LeetCode 518. 零钱兑换 II(动态规划)
1. 题目 给定不同面额的硬币和一个总金额. 写出函数来计算可以凑成总金额的硬币组合数. 假设每一种面额的硬币有无限个. 示例 1: 输入: amount = 5, coins = [1, 2, 5] ...
- leetcode 518 硬币组合种类数
给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 示例 1: 输入: amount = 5, coins = [1, 2, 5] 输出: 4 解 ...
- leetcode 518. 零钱兑换 II
难度:中等 频次:28 题目: 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额. 请你计算并返回可以凑成总金额的硬币组合数.如果任何硬币组合都无法凑出总金额, ...
- 98. Leetcode 518. 零钱兑换 II (动态规划-完全背包)
完全背包: 如果求组合数: 外层for遍历循环物品,内层for遍历循环背包容量 如果求排列数: 外层for遍历循环背包容量, 内层for遍历循环物品 步骤一.确定状态: 确定dp数组及下标含义 这里的 ...
- leetcode 518 零钱兑换II
题目 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额. 请你计算并返回可以凑成总金额的硬币组合数.如果任何硬币组合都无法凑出总金额,返回 0 . 假设每一种面 ...
- LeetCode 322. 零钱兑换(DP)
文章目录 1. 题目信息 2. 解题 2.1 回溯穷举 2.2 动态规划 1. 题目信息 给定不同面额的硬币 coins 和一个总金额 amount. 编写一个函数来计算可以凑成总金额所需的最少的硬币 ...
最新文章
- mybatis中mysql ON DUPLICATE KEY UPDATE写法
- 春招收获阿里腾讯实习offer,学习、面试经验分享
- Tomcat启动时项目重复加载,导致资源初始化两次的问题
- OpenBSD同步时间
- python中流程图的基本元素_面试干货:成为Python程序员的终极指南!(内附回答)...
- 凸优化第五章对偶 5.6扰动及灵敏度分析
- SPSS软件数据中心化、标准化和归一化
- 中山c 语言培训中心,中山英语口语培训中心
- python获取本月第一天 最后一天
- 2021“西湖论剑“网络安全大赛Writeup
- centos搭建proxypool
- Python OpenCV _1基本操作(画图,循环播放图像,鼠标事件,读取中文路径中的图片)
- OSS图片服务域名与bucket绑定域名的区别
- c#发送邮件(带抄送,密送,群发,附件)
- 安卓编程基础——手势编程
- Ubuntu操作系统安装
- 数据透视表的发明历史
- 2023年日历,高清万年历打印版
- SWT行 AWT/Swing也行系列 1 -实现半透明及不规则窗体
- 信号量解决进程的同步和互斥