Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /*
- * To execute Java, please define "static void main" on a class
- * named Solution.
- *
- * If you need more classes, simply define them inline.
- */
- class Solution {
- static final int[] COINS = new int[]{25, 10, 5, 1};
- public static List<Integer> makeChange(int amount) {
- List<Integer> ans = new ArrayList<>();
- helper(amount, ans, 0);
- return ans;
- }
- public static void helper(int amount, List<Integer> change, int i) {
- if (amount == 0 || i >= COINS.length) return;
- if (amount >= COINS[i]) {
- change.add(COINS[i]);
- helper(amount - COINS[i], change, i);
- } else {
- helper(amount, change, i + 1);
- }
- }
- public static void main(String[] args) {
- System.out.println(makeChange(0));
- System.out.println(makeChange(1));
- System.out.println(makeChange(14));
- System.out.println(makeChange(28));
- System.out.println(makeChange(36));
- System.out.println(makeChange(50));
- System.out.println(makeChange(76));
- System.out.println(makeChange(156));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement