[LintCode]最小値操作付きスタック
3629 ワード
1 class MinStack {
2 public:
3 MinStack() {
4 // do initialization if necessary
5 }
6
7 void push(int number) {
8 // write your code here
9 if (minimum.empty() || number <= minimum.top())
10 minimum.push(number);
11 data.push(number);
12 }
13
14 int pop() {
15 // write your code here
16 if (minimum.top() == data.top())
17 minimum.pop();
18 int elem = data.top();
19 data.pop();
20 return elem;
21 }
22
23 int min() {
24 // write your code here
25 return minimum.top();
26 }
27 private:
28 stack<int> data;
29 stack<int> minimum;
30 };