Given a number of different denominations of coins (e.g., 1 cent, 5 cents, 10 cents, 25 cents), get all the possible ways to pay a target number of cents.

Arguments

  • coins - an array of positive integers representing the different denominations of coins, there are no duplicate numbers and the numbers are sorted by descending order, eg. {25, 10, 5, 2, 1}
  • target - a non-negative integer representing the target number of cents, eg. 99

Assumptions

  • coins is not null and is not empty, all the numbers in coins are positive
  • target >= 0
  • You have infinite number of coins for each of the denominations, you can pick any number of the coins.

Return

  • a list of ways of combinations of coins to sum up to be target.
  • each way of combinations is represented by list of integer, the number at each index means the number of coins used for the denomination at corresponding index.

Examples

coins = {2, 1}, target = 4, the return should be

[

[0, 4],   (4 cents can be conducted by 0 * 2 cents + 4 * 1 cents)

[1, 2],   (4 cents can be conducted by 1 * 2 cents + 2 * 1 cents)

[2, 0]    (4 cents can be conducted by 2 * 2 cents + 0 * 1 cents)

]

public class Solution {
public List<List<Integer>> combinations(int target, int[] coins) {
// Write your solution here
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
helper(res, list, 0, coins, target);
return res;
} private void helper(List<List<Integer>> res, List<Integer> list, int index, int[] coins, int left) {
if (index == coins.length - 1) {
if (left % coins[coins.length - 1] == 0) {
list.add(left / coins[coins.length - 1]);
res.add(new ArrayList<>(list));
list.remove(list.size() - 1);
}
return;
}
for (int i = 0; i <= left / coins[index]; i++) {
list.add(i);
helper(res, list, index + 1, coins, left - i * coins[index]);
list.remove(list.size() - 1);
}
}
}

[Algo] 73. Combinations Of Coins的更多相关文章

  1. hdu 1398 Square Coins (母函数)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. hdu 1398 Square Coins(简单dp)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Pro ...

  3. Square Coins[HDU1398]

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. HDOJ 1398 Square Coins 母函数

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. Square Coins(母函数)

    Square Coins 点我 Problem Description People in Silverland use square coins. Not only they have square ...

  6. HDU1398 Square Coins(生成函数)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. ZOJ 1666 G-Square Coins

    https://vjudge.net/contest/67836#problem/G People in Silverland use square coins. Not only they have ...

  8. HDU 1398 Square Coins 整数拆分变形 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  9. HDU1398 Square Coins

    Description People in Silverland use square coins. Not only they have square shapes but also their v ...

随机推荐

  1. Git--rebase合并提交

    参考 https://blog.csdn.net/hj7jay/article/details/78809547 https://blog.csdn.net/yangcs2009/article/de ...

  2. dbcp连接池的一些方法

    创建连接 // 简写版: Connection conn =null; Statement st =null; conn = DBCP.getConnection(); st=conn.createS ...

  3. MySQL硬核干货:从磁盘读取数据页到缓冲池时,免费链表有什么用?

    1.数据库启动的时候,是如何初始化Buffer Pool的? 现在我们已经搞明白一件事儿了,那就是数据库的Buffer Pool到底长成个什么样,大家想必都是理解了 其实说白了,里面就是会包含很多个缓 ...

  4. delphi http server

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  5. 专为前端开发者准备的15款优秀的Sublime Text插件

    Sublime Text 已成为了目前最流行的代码编辑器之一.它的反应速度.简单易用性以及丰富的插件生态,让众多前端开发者们为之倾倒. 为了帮助开发者们更便捷地使用 Sublime Text ,我们决 ...

  6. LeetCode——221. 最大正方形

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 暴力法 ...

  7. 明明办理的是100M光纤,为何经过路由器输出只有20M?

    就在今年7月26日,宽带发展联盟发布了第20期<中国宽带速率状况报告>(2018年第二季度).报告显示,2018年第二季度我国固定宽带网络平均下载速率达到21.31Mbps,比去年第二季度 ...

  8. eclipse配置tomcat详细步骤

    1.下载tomcat9并解压到D盘根目录下 2.Windows——>Preferences——>Server——>Runtime Environments——>Add 3.选择 ...

  9. Python爬虫连载2-reponse\parse简介

    一.reponse解析 urlopen的返回对象 (1)geturl:返回网页地址 (2)info:请求反馈对象的meta信息 (3)getcode:返回的http code from urllib ...

  10. iphone对fixed模态框支持不太好,弹出窗口中滚动点击穿透的bug

    iphone对fixed展现层中存在滚动内容支持非常不好, 尤其是背景页面产生滚动以后,输入控件就找不到了, 取消冒泡也不行,最后是这么解决的,可以参考 <style> .modeldiv ...