-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path860.cpp
More file actions
29 lines (29 loc) · 834 Bytes
/
Copy path860.cpp
File metadata and controls
29 lines (29 loc) · 834 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
class Solution {
public:
bool lemonadeChange(vector<int> &bills) {
int n = bills.size();
int hold5 = 0, hold10 = 0, hold20 = 0;
for (int i = 0; i < n; ++i) {
if (bills[i] == 5)
hold5++;
else if (bills[i] == 10) {
if (hold5 > 0) {
--hold5;
++hold10;
} else
return false;
} else if (bills[i] == 20) {
if (hold5 > 0 && hold10 > 0) {
--hold5;
--hold10;
++hold20;
} else if (hold5 > 3 && hold10 == 0) {
hold5 -= 3;
++hold20;
} else
return false;
}
}
return true;
}
};