-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
55 lines (48 loc) · 1.23 KB
/
Solution.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
// Customer pay in bills {5, 10, 20}.
// Start with no bills.
// Therefore, consider the number of ways to return change to the customer
// receive 5 => no change
// receive 10 => return 5
// receive 20 => return {5, 5, 5}, {5, 10}
// Since 20 dollar bills are of no use, we dont need to track it
vector<int> remainingChange = {0, 0};
bool allCorrect = true;
for (int const& bill : bills) {
if (bill == 5) {
++remainingChange[0];
continue;
}
if (bill == 10) {
allCorrect = allCorrect && remainingChange[0]--;
++remainingChange[1];
continue;
}
if (remainingChange[0] && remainingChange[1]) {
--remainingChange[0];
--remainingChange[1];
continue;
}
if (remainingChange[0] > 2) {
remainingChange[0] -= 3;
continue;
}
// Early return
return false;
}
return allCorrect;
}
};