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. java使用BigDecimal 实现随机金额红包拆分算法

    原创代码,引用注明出处:https://www.cnblogs.com/guangxiang/p/12218714.html @Servicepublic class SplitRedPacketsS ...

  2. 编写检测深度模型测试程序python

    参考:https://blog.csdn.net/haoji007/article/details/81035565?utm_source=blogxgwz9 首先从网上下载imagenet训练好的模 ...

  3. Java算法练习——两数相加

    题目链接 题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新 ...

  4. 牛客小白月赛18——Forsaken的三维数点

    这个是一个简单题,不过因为想到比标程时间复杂度更低的方法就尝试了一下. 思路:虽然加点是三维数点,但是我们要求的是半径的大小,这样的话,就可以转变为一维的问题. 标程的解法是,用树状数组维护,然后二分 ...

  5. 对比Node.js和Python 帮你确定理想编程解决方案!

    世上没有最好的编程语言.有些编程语言比其他编程语言用于更具体的事情.比如,你可能需要移动应用程序,网络应用程序或更专业化的系统,则可能会有特定的语言.但是我们暂时假设你需要的是一个相对来说比较简单的网 ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习: JSON

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 批量导入数据表(oracle)

    批量导入数据表(oracle) 1.登陆plsql 2.找到菜单栏 工具>>导入数据>>新增图标(会提示选择*.csv文件) 选择如上图所示 3.选择数据并导入 4.下图为执行 ...

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

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

  9. iTOP-4418开发板TF卡烧写-引导uboot

    基于迅为iTOP-4418开发板 将 TF 卡接入开发板,将拨码开关设置为 TF 卡启动,进入 uboot 模式,如下图所示. 如下图所示,使用命令“fastboot”,接着就可以通过 OTG 给 e ...

  10. Graph & Tree2

    续https://www.cnblogs.com/tyqtyq/p/9769817.html 0x65 负环 SPFA 当一个节点入队次数到达N的时候,就说明有负环 或者记录最短路包含的路径条数 还有 ...