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. map构造同时初始化

    Map<String, Object> mtest =  new HashMap<String, Object>(){{put("test","M ...

  2. openstack trove 数据库镜像构建列表

    文件位置:/trove/integration/scripts/files/elements ubuntu@ubuntu:~/Downloads/trove/integration/scripts/f ...

  3. 洛谷 P2658 汽车拉力比赛

    题目传送门 解题思路: 二分答案,然后bfs验证,如果从一个路标可以达到其它所有路标,则答案可行.知道找到最佳答案. AC代码: #include<iostream> #include&l ...

  4. VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

    调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function c ...

  5. 解决: java.io.IOException: 打开的文件过多 的问题

    问题 前一阵子公司项目做了一次压力测试, 中间出现了一个问题: 在50多个并发的时候会出现 java.io.IOException: 打开的文件过多 这个异常. 但是在没有并发的时候是不会出现这个问题 ...

  6. 微信小程序自定义分享封面

    onShareAppMessage:function(options){ let thas = this; if (options.from === 'button') { // 来自页面内转发按钮 ...

  7. Vue-router(5)之 路由的before家族

    beforeEach方法 import Vue from 'vue' import Router from 'vue-router' import Son1 from '@/view/New/son1 ...

  8. textField 基本属性

    _textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textField.text = str; [_t ...

  9. XML技术详解

    XML 1.XML概述 XML可扩展标记语言是一种基于文本的语言用作应用程序之间的通信模式,是一个非常有用的描述结构化信息的技术.XML工具使得转化和处理数据变得十分容易,但同样也要领域相关的标准和代 ...

  10. iOS 内购相关

    iOS 内购相关 下面总结一下过往订阅和内购的项目的代码方面的实现细节和注意事项,特别是掉单方面的处理. 后台的协议.商品ID.银行卡.内购类型.沙盒账号测试人员都由运营或者产品在苹果后台中申请处理. ...