-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlemonadeChange.java
More file actions
31 lines (29 loc) · 1.19 KB
/
lemonadeChange.java
File metadata and controls
31 lines (29 loc) · 1.19 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
class Solution {
public boolean lemonadeChange(int[] bills) {
int fiveDollarCount = 0; // Count of $5 bills
int tenDollarCount = 0; // Count of $10 bills
for (int bill : bills) {
if (bill == 5) {
fiveDollarCount++; // Increase count for $5 bill
} else if (bill == 10) {
if (fiveDollarCount > 0) {
fiveDollarCount--; // Provide $5 change
tenDollarCount++; // Increase count for $10 bill
} else {
return false; // Cannot provide change
}
} else if (bill == 20) {
// Prefer to give one $10 and one $5 if possible
if (tenDollarCount > 0 && fiveDollarCount > 0) {
tenDollarCount--; // Use one $10 bill
fiveDollarCount--; // Use one $5 bill
} else if (fiveDollarCount >= 3) {
fiveDollarCount -= 3; // Use three $5 bills
} else {
return false; // Cannot provide change
}
}
}
return true; // Successfully provided change to all customers
}
}