forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (50 loc) · 1.38 KB
/
main.cpp
File metadata and controls
64 lines (50 loc) · 1.38 KB
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
/// Source : https://leetcode.com/problems/lemonade-change/description/
/// Author : liuyubobobo
/// Time : 2018-06-30
#include <iostream>
#include <vector>
using namespace std;
/// Simulation and Greedy
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0, ten = 0, twenty = 0;
for(int bill: bills){
if(bill == 5)
five ++;
else if(bill == 10){
ten ++;
if(five == 0)
return false;
else
five --;
}
else{
twenty ++;
if(ten > 0 && five > 0)
ten --, five --;
else if(five >= 3)
five -= 3;
else
return false;
}
}
return true;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<int> bills1 = {5, 5, 5, 10, 20};
print_bool(Solution().lemonadeChange(bills1));
vector<int> bills2 = {5, 5, 10};
print_bool(Solution().lemonadeChange(bills2));
vector<int> bills3 = {10, 10};
print_bool(Solution().lemonadeChange(bills3));
vector<int> bills4 = {5, 5, 10, 10, 20};
print_bool(Solution().lemonadeChange(bills4));
return 0;
}