blob: 48550be8d0da0a672726f69215af22ce58ec22d5 (
plain)
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
|
#include <vector>
#include <set>
class MinStack
{
public:
/** initialize your data structure here. */
MinStack()
{
}
void push(int x)
{
data_.push_back(x);
sorted_data_.insert(x);
}
void pop()
{
const auto v = data_.back();
data_.pop_back();
sorted_data_.erase(sorted_data_.find(v));
}
int top()
{
return data_.back();
}
int getMin()
{
return *sorted_data_.cbegin();
}
private:
std::vector<int> data_;
std::multiset<int> sorted_data_;
};
|