Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ATM {
- int[] banknotesCount;
- private final int[] coin;
- public ATM() {
- banknotesCount = new int[5];
- coin = new int[] {20, 50, 100, 200, 500};
- }
- public void deposit(int[] banknotes) {
- for (int i = 0; i < 5; i++) {
- this.banknotesCount[i] += banknotes[i];
- }
- }
- public int[] withdraw(int amount) {
- int[] withdrawCount = new int[5];
- for (int i = banknotesCount.length - 1; i >= 0; i--) {
- int count = (int) Math.min(banknotesCount[i], amount / coin[i]);
- amount -= coin[i] * count;
- withdrawCount[i] = count;
- }
- if (amount != 0) {
- return new int[] {-1};
- } else {
- for (int i = 0; i < 5; i++) {
- banknotesCount[i] -= withdrawCount[i];
- }
- return withdrawCount;
- }
- }
- // 700 = 500 * 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement