forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_stack_hump_n.cpp
51 lines (42 loc) · 992 Bytes
/
AC_stack_hump_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_hump_n.cpp
* Create Date: 2014-12-21 08:36:17
* Descripton: use an array to simulate->TLE
* use two arrays to simulate->MLE
* use two vectors to sismulate->MLE
* use two stack to simulate->MLE
*
* Correct solution:
* record only the min elements in minstack,
* just like hump
* use O(n) time, O(n) space
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class MinStack {
private:
stack<int> stk;
stack<int> mstk;
public:
void push(int x) {
if (mstk.empty() || x <= mstk.top())
mstk.push(x);
stk.push(x);
}
void pop() {
if (stk.top() == mstk.top())
mstk.pop();
stk.pop();
}
int top() {
return stk.top();
}
int getMin() {
return mstk.top();
}
};
int main() {
return 0;
}