|
1 | 1 | # [Problem 860: Lemonade Change](https://leetcode.com/problems/lemonade-change/description/?envType=daily-question)
|
2 | 2 |
|
3 | 3 | ## Initial thoughts (stream-of-consciousness)
|
| 4 | +- What a breath of fresh air after yesterday's problem. |
| 5 | +- This one seems very easy. We could either solve this by using a list to track all of the bills in our collection, or (more efficiently) we could use a hash table to track the numbers of each possible type of bill. There are only three, so we can initialize `register = {5: 0, 10: 0, 20: 0}`. |
| 6 | +- Then we just step through one bill at a time, updating `register` as needed. The amount of change is just `bill[i] - 5`. |
| 7 | +- There are just a few options for how much change might be needed: 0, 5, or 15: |
| 8 | + - No change is needed if someone pays with a $5 bill. There's no need to check the register, but we update `register[5] += 1`. |
| 9 | + - $5 in change is needed if someone pays with $10. Update `register[10] += 1` and decrement `register[5] -= 1` (or return false if that dips `register[5]` below 0. |
| 10 | + - $15 in change is needed if someone pays with a $20. This requires one of the following: |
| 11 | + - 1 $10 and 1 $5 bill if available |
| 12 | + - 3 $5 bills otherwise |
| 13 | + - We should always prefer giving change with larger bills, because this preserves our flexibility in later transactions |
4 | 14 |
|
5 | 15 | ## Refining the problem, round 2 thoughts
|
| 16 | +- No edge cases I can think of...let's implement it! |
6 | 17 |
|
7 | 18 | ## Attempted solution(s)
|
8 | 19 | ```python
|
9 |
| -class Solution: # paste your code here! |
10 |
| - ... |
| 20 | +class Solution: |
| 21 | + def lemonadeChange(self, bills: List[int]) -> bool: |
| 22 | + cost = 5 |
| 23 | + register = {5: 0, 10: 0, 20: 0} |
| 24 | + |
| 25 | + for payment in bills: |
| 26 | + register[payment] += 1 |
| 27 | + change = payment - cost |
| 28 | + if change == 0: |
| 29 | + continue |
| 30 | + elif change == 5: |
| 31 | + if register[5] <= 0: |
| 32 | + return False |
| 33 | + register[5] -= 1 |
| 34 | + elif change == 15: |
| 35 | + if register[10] > 0 and register[5] > 0: # try making change with $10 and $5 |
| 36 | + register[10] -= 1 |
| 37 | + register[5] -= 1 |
| 38 | + elif register[5] >= 3: # try making change with 3 $5 bills |
| 39 | + register[5] -= 3 |
| 40 | + else: # correct change is not available |
| 41 | + return False |
| 42 | + |
| 43 | + return True |
11 | 44 | ```
|
| 45 | +- Given test cases pass |
| 46 | +- Let's make up some new test cases: |
| 47 | + - `bills = [10, 5, 10, 5, 10, 20, 5, 5, 10, 5, 5, 5, 20, 20, 20, 10, 10, 10, 10, 5, 10, 10, 10, 10, 20, 5, 20, 10, 10, 10, 20, 10, 10, 5, 5, 20, 20, 5, 5, 10, 10, 5, 5, 5, 5, 10, 10, 20, 5, 5, 10, 20, 20, 5, 20, 5, 20, 10, 5, 20, 20, 5, 5, 5, 10, 5, 20, 10, 10, 10, 5, 10, 5, 5, 10, 20, 10, 10, 20, 10, 20, 20, 20, 10, 20, 5, 20, 5, 10, 10, 20, 5, 5, 20, 20, 5, 5, 10, 20, 20]`: pass |
| 48 | + - `bills = [5, 10, 5, 10, 5, 10, 20, 5, 5, 10, 5, 5, 5, 20, 20, 20, 10, 10, 10, 10, 5, 10, 10, 10, 10, 20, 5, 20, 10, 10, 10, 20, 10, 10, 5, 5, 20, 20, 5, 5, 10, 10, 5, 5, 5, 5, 10, 10, 20, 5, 5, 10, 20, 20, 5, 20, 5, 20, 10, 5, 20, 20, 5, 5, 5, 10, 5, 20, 10, 10, 10, 5, 10, 5, 5, 10, 20, 10, 10, 20, 10, 20, 20, 20, 10, 20, 5, 20, 5, 10, 10, 20, 5, 5, 20, 20, 5, 5, 10, 20, 20]`: pass |
| 49 | + - `bills = [5, 5, 10, 20, 20, 10, 10, 20, 20, 5, 5, 10, 20, 10, 20, 5, 10, 10, 20, 5]`: pass |
| 50 | + - `bills = [5, 5, 5, 5, 10, 20, 5, 5, 5, 5, 10, 5, 10, 10, 10, 20, 5]`: pass |
| 51 | +- Ok...seems like it's working; let's submit... |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +Solved! |
| 56 | + |
0 commit comments