forked from algorithm010/algorithm010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC850_lemonade_change.java
More file actions
36 lines (34 loc) · 977 Bytes
/
LC850_lemonade_change.java
File metadata and controls
36 lines (34 loc) · 977 Bytes
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
package Week04;
public class LC850_lemonade_change {
/*
* 1.loop through all bill
* 2.count how many 5, 10, 20 bills
* 3.for $5 just reduce the count, $10 reduce one $5 count,
* greedy: for $20 try to reduce one $5 and one $10 count, if not able, reduce 3 of $5
* 4.if don't have enough count return false
* */
public boolean lemonadeChange(int[] bills) {
int c5 = 0, c10 = 0;
for (int b : bills) {
if (b == 5) {
c5++;
} else if (b == 10) {
if (c5 == 0) {
return false;
}
c5--;
c10++;
} else {
if (c10 > 0 && c5 > 0) {
c10--;
c5--;
} else if (c5 >= 3) {
c5 -= 3;
} else {
return false;
}
}
}
return true;
}
}