-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-860-Lemonade_Change.cpp
66 lines (55 loc) · 1.63 KB
/
leetcode-860-Lemonade_Change.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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>
bool lemonadeChange(std::vector<int>& bills) {
std::unordered_map<int, size_t> count_appears = {{5, 0},
{10, 0}};
for(size_t i = 0; i<bills.size(); ++i)
{
if(i == 0 && bills[i] != 5)
return false;
switch (bills[i])
{
case 5:
{
++count_appears[bills[i]];
continue;
}
case 10:
{
if(count_appears[5])
{
++count_appears[bills[i]];
--count_appears[5];
break;
}
return false;
}
case 20:
{
if(count_appears[5] >= 1 && count_appears[10] >= 1) {
count_appears[5] -= 1;
count_appears[10] -= 1;
break;
} else if(count_appears[5] >= 3) {
count_appears[5] -= 3;
break;
}
return false;
}
}
}
return true;
}
int main(){
// std::vector<int> vec = {5,5,5,10,20};
// std::vector<int> vec = {5,5,10,10,20};
// std::vector<int> vec = {5,5,5,5,10,5,20,10,5,5};
// std::vector<int> vec = {5,5,5,5,10,5,10,10,10,20};
// std::vector<int> vec = {5,5,5,10,5,5,10,20,20,20};
std::vector<int> vec = {5,5,5,5,20,20,5,5,5,5};
auto res = lemonadeChange(vec);
std::cout << res << std::endl;
return EXIT_SUCCESS;
}