-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path30_min_in_stack.cc
61 lines (51 loc) · 981 Bytes
/
30_min_in_stack.cc
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
#include <iostream>
#include <stack>
#include "macro_util.h"
using namespace std;
class Solution {
public:
void push(int value) {
s_.push(value);
if (min_s_.empty()) {
min_s_.push(value);
} else {
if (value < min_s_.top()) {
min_s_.push(value);
} else {
min_s_.push(min_s_.top());
}
}
}
void pop() {
if (!s_.empty()) {
s_.pop();
min_s_.pop();
}
}
int top() {
if (s_.empty()) {
return -1;
}
return s_.top();
}
int min() {
if (min_s_.empty()) {
return -1;
}
return min_s_.top();
}
private:
stack<int> s_;
stack<int> min_s_;
};
int main(int argc, char *argv[])
{
Solution s;
s.push(1);
cout << s.min() << endl;
s.pop();
s.push(3);
s.push(5);
cout << s.min() << endl;
return 0;
}