|
| 1 | +/* |
| 2 | + * @author Samy Rezzag |
| 3 | + */ |
| 4 | + |
| 5 | +/* |
| 6 | + * I believe with my current implementation -- I cannot guarantee an optimal result, |
| 7 | + * as is the nature of greedy algorithms, it can be optimal under the right circumstances. |
| 8 | + * For some parameters such as [1,2,4,8,2^n], this implementation will guarantee the optimal |
| 9 | + * result because the values are spaced out well enough, as are the ones provided for sample input. |
| 10 | + * |
| 11 | + * For example, one instance where it won't be optimal is the following: |
| 12 | + * |
| 13 | + * currencies[] = [1, 9, 18, 36, 45] where amount = 73. |
| 14 | + * My implementation would return output[] = [1,1,1,0,1] (1+9+18+45) |
| 15 | + * As compared to the *optimal* output[] = [1,0,0,2,0] (1+36+36) |
| 16 | + */ |
| 17 | +public class MoneyDispenser { |
| 18 | + |
| 19 | + private int[] currencies; // Input Array |
| 20 | + private int[] output; // Output Array |
| 21 | + private int counter; // Keep track of how many of a certain bill is used. |
| 22 | + private int total = 0; // Used to verify we are dispensing the right amount. |
| 23 | + private int initialAmount = 0; // Used to verify we are dispensing the right amount. |
| 24 | + |
| 25 | + /* |
| 26 | + * @param int[] currencies is an input array that is pre-sorted and contains the |
| 27 | + * available denominations. |
| 28 | + * |
| 29 | + * This is a constructor which initializes the currencies array and creates an |
| 30 | + * output array of the same length. |
| 31 | + */ |
| 32 | + public MoneyDispenser(int[] currencies) { |
| 33 | + this.currencies = currencies; |
| 34 | + output = new int[currencies.length]; |
| 35 | + } |
| 36 | + |
| 37 | + /* |
| 38 | + * @param amount is the amount of money a user is looking to receive. |
| 39 | + * |
| 40 | + * This first copies the amount to initalAmount. This is because "amount" will |
| 41 | + * change throughout the method. |
| 42 | + * |
| 43 | + * Next we check to see if the amount is less than 0. If so, fill the output |
| 44 | + * array with 0's and return it. |
| 45 | + * |
| 46 | + * Then, we run a loop. For each entry in the input array, from right to left, |
| 47 | + * subtract that value from "amount" if "amount" is greater. it also adds on to |
| 48 | + * a running total for verification later on that we are returning the equal |
| 49 | + * amount. It also has a counter which keeps track of how many of that bill is |
| 50 | + * being given out. |
| 51 | + * |
| 52 | + * Next, for each iteration of that, we add the counter amount to the output |
| 53 | + * array in the same index that was used for the currencies array. |
| 54 | + * |
| 55 | + * Finally, we perform a quick check to ensure that we've reached the end of the |
| 56 | + * array and are capable of giving back exact change. If not, fill output with |
| 57 | + * 0's and return the array. |
| 58 | + */ |
| 59 | + public int[] dispense(int amount) { |
| 60 | + initialAmount = amount; |
| 61 | + if (amount < 0) { |
| 62 | + for (int i = 0; i < currencies.length; i++) { |
| 63 | + output[i] = 0; |
| 64 | + } |
| 65 | + return output; |
| 66 | + } |
| 67 | + for (int i = currencies.length - 1; i >= 0; i--) { |
| 68 | + counter = 0; |
| 69 | + while (amount >= currencies[i]) { |
| 70 | + amount = amount - currencies[i]; |
| 71 | + total += currencies[i]; |
| 72 | + counter++; |
| 73 | + } |
| 74 | + output[i] = counter; |
| 75 | + } |
| 76 | + if (total != initialAmount) { |
| 77 | + for (int i = 0; i < currencies.length; i++) { |
| 78 | + output[i] = 0; |
| 79 | + } |
| 80 | + } |
| 81 | + return output; |
| 82 | + } |
| 83 | + |
| 84 | + public static void main(String[] args) { |
| 85 | + |
| 86 | + MoneyDispenser m1 = new MoneyDispenser(new int[] { 1, 5, 10, 20, 50, 100 }); |
| 87 | + m1.dispense(0); |
| 88 | + MoneyDispenser m2 = new MoneyDispenser(new int[] { 1, 5, 25, 100, 500 }); |
| 89 | + m2.dispense(841); |
| 90 | + MoneyDispenser m3 = new MoneyDispenser(new int[] { 1, 9, 18, 36, 45 }); |
| 91 | + m3.dispense(73); |
| 92 | + MoneyDispenser m4 = new MoneyDispenser(new int[] { 1, 5, 25, 100, 500 }); |
| 93 | + m4.dispense(-1); |
| 94 | + } |
| 95 | +} |
0 commit comments