[LeetCode] Lemonade Change

860. Lemonade Change

At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

Note that you do not have any change in hand at first.

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int b5 = 0, b10 = 0;
for(auto& b : bills) {
if(b == 5) {
b5++;
} else if(b == 10) {
if(!b5) return false;
b5--;
b10++;
} else {
if(b10 and b5) {
b10--;
b5--;
} else if(b5 >= 3) {
b5 -= 3;
} else return false;
}
}

return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/06/PS/LeetCode/lemonade-change/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.