-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLemonadeChange.java
More file actions
39 lines (37 loc) · 1.4 KB
/
LemonadeChange.java
File metadata and controls
39 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 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.
*/
class LemonadeChange {
public boolean lemonadeChange(int[] theBills) {
int count5 = 0; // count of $5 bills in hand
int count10 = 0; // count of $10 bills in hand
for (int bill : theBills) {
if (bill == 5)
count5++;
else if (bill == 10) {
if (count5 > 0) {
count5--;
count10++;
} else
return false;
// bill == 20
} else {
if (count10 > 0 && count5 > 0) {
count10--;
count5--;
} else if (count5 >= 3)
count5 -= 3;
else
return false;
}
}
return true;
}
}