Skip to content

Commit c691cea

Browse files
authored
Update lemonade-change.cpp
1 parent 112478f commit c691cea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

C++/lemonade-change.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22
// Space: O(1)
33

44
class Solution {
5+
public:
6+
bool lemonadeChange(vector<int>& bills) {
7+
static const vector<int> coins = {20, 10, 5};
8+
unordered_map<int, int> counts;
9+
for (const auto& bill : bills) {
10+
++counts[bill];
11+
auto change = bill - coins.back();
12+
for (const auto& coin : coins) {
13+
if (change == 0) {
14+
break;
15+
}
16+
if (change >= coin) {
17+
const auto count = min(counts[coin], change / coin);
18+
counts[coin] -= count;
19+
change -= coin * count;
20+
}
21+
}
22+
if (change != 0) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
};
29+
30+
class Solution2 {
531
public:
632
bool lemonadeChange(vector<int>& bills) {
733
int five = 0, ten = 0;

0 commit comments

Comments
 (0)