-
Notifications
You must be signed in to change notification settings - Fork 0
/
minst.cpp
62 lines (51 loc) · 1.11 KB
/
minst.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
//
// minst.cpp
// xcode
//
// Created by Gokul Nadathur on 9/13/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <stack>
#include <climits>
using namespace std;
class MinStack {
public:
struct trackMin {
int val;
int index;
trackMin() : val(0), index(0) {}
trackMin(int v, int i) : val(v), index(i) {}
};
stack<int> vals;
stack<trackMin> mins;
int currentMin;
/** initialize your data structure here. */
MinStack() {
currentMin = INT_MAX;
}
void push(int x) {
vals.push(x);
if (x <= currentMin) {
currentMin = x;
mins.push(trackMin(x,vals.size()));
}
}
void pop() {
auto mintop = mins.top();
if (vals.size() == mintop.index) {
mins.pop();
currentMin = mins.size() != 0 ? mins.top().val : INT_MAX;
}
vals.pop();
}
int top() {
return vals.top();
}
int getMin() {
return mins.top().val;
}
};
int main(int argc, char** argv)
{
return 0;
}