Leetcode: Coin Change
来源:互联网 发布:软件著作规定 编辑:程序博客网 时间:2024/04/30 00:30
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.
类似于经典的DP Coin change求最多的组合次数,这里求最小的硬币总数。采用由底往上的解法,从0开始。
class Solution {public: int coinChange(vector<int>& coins, int amount) { sort(coins.begin(), coins.end()); vector<int> minCounts(amount + 1, INT_MAX); minCounts[0] = 0; for (int i = 1; i <= amount; ++i) { for (int j = 0; j < coins.size(); ++j) { if (i < coins[j]) { break; } if (minCounts[i-coins[j]] != INT_MAX) { minCounts[i] = min(minCounts[i], minCounts[i-coins[j]] + 1); } } } return minCounts[amount] == INT_MAX ? -1 : minCounts[amount]; }};
0 0
- leetcode Coin Change
- Leetcode: Coin Change
- LeetCode Coin Change
- [leetcode] 322. Coin Change
- Leetcode: Coin Change
- [LeetCode 322] Coin Change
- leetcode--Coin Change
- [leetcode]Coin Change
- leetcode 322. Coin Change
- leetcode:Coin Change
- leetcode之Coin Change
- Leetcode 322. Coin Change
- Leetcode 322. Coin Change
- leetcode笔记:Coin Change
- leetcode 322 Coin Change
- 322. Coin Change LeetCode
- 【LeetCode-322】Coin Change
- leetcode之 coin change
- 为什么windows要使用消息机制.
- 算法导论——选择排序
- 数据库到底用不用外键
- linux任务组物理资源限制机制cgroup
- C#winform webBrowser 实现抓句柄 想输入框中输入内容(笔记)
- Leetcode: Coin Change
- BZOJ3343: 教主的魔法
- Android空间架构与自定义控件详解-更新中
- 进入cell的详情页
- Linux中的swap分区
- 利用Nginx的上传模块和上传进度模块实现网页上传文件
- 欢迎使用CSDN-markdown编辑器
- 转载一些Java基础题
- C51 单片机矩阵键盘