|
| 1 | +package com.laioffer.dfs; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class _99Cents { |
| 7 | + |
| 8 | + /** |
| 9 | + * Given a number of different denominations of coins (e.g., 1 cent, 5 cents, 10 cents, 25 cents), |
| 10 | + * get all the possible ways to pay a target number of cents. |
| 11 | + * |
| 12 | + * <p>Arguments |
| 13 | + * |
| 14 | + * <p>coins - an array of positive integers representing the different denominations of coins, |
| 15 | + * there are no duplicate numbers and the numbers are sorted by descending order, eg. {25, 10, 5, |
| 16 | + * 2, 1} target - a non-negative integer representing the target number of cents, eg. 99 |
| 17 | + * Assumptions |
| 18 | + * |
| 19 | + * <p>coins is not null and is not empty, all the numbers in coins are positive target >= 0 You |
| 20 | + * have infinite number of coins for each of the denominations, you can pick any number of the |
| 21 | + * coins. Return |
| 22 | + * |
| 23 | + * <p>a list of ways of combinations of coins to sum up to be target. each way of combinations is |
| 24 | + * represented by list of integer, the number at each index means the number of coins used for the |
| 25 | + * denomination at corresponding index. Examples |
| 26 | + * |
| 27 | + * <p>coins = {2, 1}, target = 4, the return should be |
| 28 | + * |
| 29 | + * <p>[ |
| 30 | + * |
| 31 | + * <p>[0, 4], (4 cents can be conducted by 0 * 2 cents + 4 * 1 cents) |
| 32 | + * |
| 33 | + * <p>[1, 2], (4 cents can be conducted by 1 * 2 cents + 2 * 1 cents) |
| 34 | + * |
| 35 | + * <p>[2, 0] (4 cents can be conducted by 2 * 2 cents + 0 * 1 cents) |
| 36 | + * |
| 37 | + * <p>] |
| 38 | + */ |
| 39 | + public static void main(String[] args) { |
| 40 | + int[] coin = new int[] {25, 10, 5, 1}; |
| 41 | + findCoinCombination(coin, 99, 0, new int[4]); |
| 42 | + List<List<Integer>> result = combinations(99, coin); |
| 43 | + } |
| 44 | + |
| 45 | + public static List<List<Integer>> combinations(int target, int[] coins) { |
| 46 | + List<List<Integer>> result = new ArrayList<List<Integer>>(); |
| 47 | + List<Integer> cur = new ArrayList<Integer>(); |
| 48 | + helper(target, coins, 0, cur, result); |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + private static void helper( |
| 53 | + int target, int[] coins, int index, List<Integer> cur, List<List<Integer>> result) { |
| 54 | + if (index == coins.length - 1) { |
| 55 | + if (target % coins[coins.length - 1] == 0) { |
| 56 | + cur.add(target / coins[coins.length - 1]); |
| 57 | + result.add(new ArrayList<Integer>(cur)); |
| 58 | + cur.remove(cur.size() - 1); |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + int max = target / coins[index]; |
| 63 | + for (int i = 0; i <= max; i++) { |
| 64 | + cur.add(i); |
| 65 | + helper(target - i * coins[index], coins, index + 1, cur, result); |
| 66 | + cur.remove(cur.size() - 1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static void findCoinCombination(int[] coin, int moneyLeft, int index, int[] sol) { |
| 71 | + if (index == 3) { |
| 72 | + sol[index] = moneyLeft; |
| 73 | + printSol(sol, coin); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + for (int i = 0; i <= moneyLeft / coin[index]; i++) { |
| 78 | + sol[index] = i; |
| 79 | + findCoinCombination(coin, moneyLeft - i * coin[index], index + 1, sol); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void printSol(int[] sol, int[] coin) { |
| 84 | + StringBuilder sb = new StringBuilder(); |
| 85 | + for (int i = 0; i < sol.length; i++) { |
| 86 | + sb.append(sol[i]); |
| 87 | + sb.append('x'); |
| 88 | + sb.append(coin[i]); |
| 89 | + sb.append(" cent "); |
| 90 | + } |
| 91 | + System.out.println(sb.toString()); |
| 92 | + } |
| 93 | +} |
0 commit comments