Battery (Coin Change)

Problem

电池有6节包装,9节包装,20节包装三种,input需要多少节电池,如果可以刚好用3种包装的凑到这个数,就输出这个解, 忘了是不是要输出所有的解。
e.g 输入20, 答{20} 输入17 答没有 输入18,那可能是{6,6,6}也可能是{9,9}。 有点像找钱的问题,似乎是从集合中找到所有集合值等于一个target这个题的简化版,因为集合只有6 9 20。

Solution

 1 public static List<List<Integer>> combination(int[] num, int target) {
 2     List<List<Integer>> res = new ArrayList<List<Integer>>();
 3
 4     if(num == null || num.length == 0) return res;
 5
 6     List<Integer> path = new ArrayList<Integer>();
 7     helper(num, target, res, path);
 8     return res;
 9
10 }
11
12 public static void helper(int[] num, int target, List<List<Integer>> res, List<Integer> path) {
13
14     if(target == 0) {
15         res.add(new ArrayList<Integer>(path));
16         return;
17     }
18
19     if(target < 0)
20         return;
21
22     for(int i=0; i<num.length; i++) {
23         path.add(num[i]);
24         helper(num, target - num[i], res, path);
25         path.remove(path.size() - 1);
26     }
27 }
时间: 2024-10-31 02:24:56

Battery (Coin Change)的相关文章

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

[LeetCode][JavaScript]Coin Change

Coin Change 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 o

Leetcode OJ --- 322. Coin Change

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,

HDU 2069 Coin Change

Coin Change Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 206964-bit integer IO format: %I64d      Java class name: Main Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We

HDU 2069 Coin Change 母函数求解

HDU 2069 Coin Change 母函数求解 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2069 这题比普通的母函数求解题目复杂一点,多了组成个数的限制, 要求组合个数不能超过 100 个硬币,所以将 c1, c2 定义为二维数组, c1[i][j] 表示 c1[结果][组成该结果的个数] ,然后多了一层遍历个数的循环. // 母函数求解 #include <bits/stdc++.h> using namespace std; cons

hdu 2069 Coin Change(完全背包)

Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16592 Accepted Submission(s): 5656 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent.

Coin Change

322. Coin Change Total Accepted: 4182 Total Submissions: 16210 Difficulty: Medium 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

UVa 674 Coin Change (经典DP)

Coin Change Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. For example

HDU2069 Coin Change 【暴力】

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13500    Accepted Submission(s): 4510 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and