|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * 860. Lemonade Change |
| 8 | + * |
| 9 | + * At a lemonade stand, each lemonade costs $5. |
| 10 | + * |
| 11 | + * Customers are standing in a queue to buy from you, |
| 12 | + * and order one at a time (in the order specified by bills). |
| 13 | + * Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. |
| 14 | + * You must provide the correct change to each customer, |
| 15 | + * so that the net transaction is that the customer pays $5. |
| 16 | + * Note that you don't have any change in hand at first. |
| 17 | + * Return true if and only if you can provide every customer with correct change. |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * Input: [5,5,5,10,20] |
| 21 | + * Output: true |
| 22 | + * Explanation: |
| 23 | + * From the first 3 customers, we collect three $5 bills in order. |
| 24 | + * From the fourth customer, we collect a $10 bill and give back a $5. |
| 25 | + * From the fifth customer, we give a $10 bill and a $5 bill. |
| 26 | + * Since all customers got correct change, we output true. |
| 27 | + * |
| 28 | + * Example 2: |
| 29 | + * Input: [5,5,10] |
| 30 | + * Output: true |
| 31 | + * |
| 32 | + * Example 3: |
| 33 | + * Input: [10,10] |
| 34 | + * Output: false |
| 35 | + * |
| 36 | + * Example 4: |
| 37 | + * Input: [5,5,10,10,20] |
| 38 | + * Output: false |
| 39 | + * Explanation: |
| 40 | + * From the first two customers in order, we collect two $5 bills. |
| 41 | + * For the next two customers in order, we collect a $10 bill and give back a $5 bill. |
| 42 | + * For the last customer, we can't give change of $15 back because we only have two $10 bills. |
| 43 | + * Since not every customer received correct change, the answer is false. |
| 44 | + * |
| 45 | + * |
| 46 | + * Note: |
| 47 | + * 0 <= bills.length <= 10000 |
| 48 | + * bills[i] will be either 5, 10, or 20. |
| 49 | + */ |
| 50 | +public class _860 { |
| 51 | + public static class Solution1 { |
| 52 | + public boolean lemonadeChange(int[] bills) { |
| 53 | + Map<Integer, Integer> map = new HashMap<>(); |
| 54 | + for (int bill : bills) { |
| 55 | + if (bill == 5) { |
| 56 | + map.put(5, map.getOrDefault(5, 0) + 1); |
| 57 | + } else if (bill == 10) { |
| 58 | + if (!map.containsKey(5)) { |
| 59 | + return false; |
| 60 | + } else { |
| 61 | + map.put(5, map.get(5) - 1); |
| 62 | + if (map.get(5) == 0) { |
| 63 | + map.remove(5); |
| 64 | + } |
| 65 | + map.put(10, map.getOrDefault(10, 0) + 1); |
| 66 | + } |
| 67 | + } else { |
| 68 | + if (!map.containsKey(5)) { |
| 69 | + return false; |
| 70 | + } else { |
| 71 | + if (!map.containsKey(10)) { |
| 72 | + if (!map.containsKey(5) || map.get(5) < 3) { |
| 73 | + return false; |
| 74 | + } else { |
| 75 | + map.put(5, map.get(5) - 3); |
| 76 | + if (map.get(5) == 0) { |
| 77 | + map.remove(5); |
| 78 | + } |
| 79 | + } |
| 80 | + } else { |
| 81 | + if (!map.containsKey(5)) { |
| 82 | + return false; |
| 83 | + } else { |
| 84 | + map.put(5, map.get(5) - 1); |
| 85 | + if (map.get(5) == 0) { |
| 86 | + map.remove(5); |
| 87 | + } |
| 88 | + map.put(10, map.get(10) - 1); |
| 89 | + if (map.get(10) == 0) { |
| 90 | + map.remove(10); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + map.put(20, map.getOrDefault(20, 0) + 1); |
| 96 | + } |
| 97 | + } |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments