Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /*
  5.  * To execute Java, please define "static void main" on a class
  6.  * named Solution.
  7.  *
  8.  * If you need more classes, simply define them inline.
  9.  */
  10.  
  11. class Solution {
  12.  
  13.   static final int[] COINS = new int[]{25, 10, 5, 1};
  14.  
  15.   public static List<Integer> makeChange(int amount) {
  16.         List<Integer> ans = new ArrayList<>();
  17.         helper(amount, ans, 0);
  18.         return ans;
  19.   }  
  20.  
  21.   public static void helper(int amount, List<Integer> change, int i) {
  22.       if (amount == 0 || i >= COINS.length) return;
  23.       if (amount >= COINS[i]) {
  24.           change.add(COINS[i]);
  25.           helper(amount - COINS[i], change, i);
  26.       } else {
  27.           helper(amount, change, i + 1);
  28.       }
  29.   }
  30.  
  31.   public static void main(String[] args) {
  32.       System.out.println(makeChange(0));
  33.       System.out.println(makeChange(1));
  34.       System.out.println(makeChange(14));
  35.       System.out.println(makeChange(28));
  36.       System.out.println(makeChange(36));
  37.       System.out.println(makeChange(50));
  38.       System.out.println(makeChange(76));
  39.       System.out.println(makeChange(156));
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement