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. python *args 和 ** kwargs

    可变长度的参数 *args的使用方法 *args 用来将参数打包成tuple给函数体调用 可见,1这个参数,被打包成了一个元组 def func(*args): print(args,type(arg ...

  2. delphi save .dfm to .txt

    procedure TForm2.saveDfm; var inStream,outStream:TMemoryStream; begin inStream:=TMemoryStream.Create ...

  3. UVA_10603 倒水问题 隐式图搜索

    这道题目是刘汝佳白书上的例题,没有LRJ在白书上提到的划归为搜索问题,还真是一时难以想到好的解法.即三个瓶子,任意的一个状态都是一个节点,最后就划归为一个搜索问题. 由于题目数据量不大,三个杯子容量都 ...

  4. linux环境java程序cpu爆表问题查证

    1.top命令查找导致cup爆表的进程 2. top -H -p10832 (10832是Java进程的PID)命令找出了具体的线程 3.使用用命令 jstack 10832> jstack.t ...

  5. 洛谷 P1341 无序字母对(欧拉回路)

    题目传送门 解题思路: 一道欧拉回路的模板题,详细定理见大佬博客,任意门 AC代码: #include<cstdio> #include<iostream> using nam ...

  6. Python笔记_第四篇_高阶编程_正则表达式_1.正则表达式简介(re模块)

    1. 从一个判断手机号的问题引入: 如果给你一个字符串,去判断是否是一个手机号码,我们通过之前的学习可以有如下代码: # 如果用普通的方式去检验一个电话号码非常麻烦. def checkPhone(s ...

  7. empty和is_null以及isset函数在0、”0”、‘空串’、NULL、false、array()的计算值

    1empty:只要是非空或者非零的值都返回false,换句话说‘’.‘0’.0.null.false都返回true: 2is_null: 当参数满足下面三种情况时,is_null()将返回TRUE,其 ...

  8. jsp动作标签学习

    <jsp:useBean> <jsp:useBean>标签用于在指定的域范围内查找指定名称的JavaBean对象,如果存在则直接返回该JavaBean对象的引用,如果不存在则实 ...

  9. cin cout

    编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置.当用户按下enter键,数据输入停止.程序自动对所有的整数进行求和并打印出结果. 需要解决两个 ...

  10. AUTOSAR-Specification of Watchdog Manager 阅读

    一.开门狗管理有三种机制 1.定周期任务实时监控 2.非定周期任务执行时间监控 3.逻辑监控,执行顺序. 二.受监控的实体和检查点 Watchdog Manager监督软件的执行.监督的逻辑单位是受监 ...