We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 112478f commit c691ceaCopy full SHA for c691cea
C++/lemonade-change.cpp
@@ -2,6 +2,32 @@
2
// Space: O(1)
3
4
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 {
31
public:
32
bool lemonadeChange(vector<int>& bills) {
33
int five = 0, ten = 0;
0 commit comments