aboutsummaryrefslogtreecommitdiff
path: root/min_stack.cpp
blob: 745c537b4cfaf91728515fcfb09cdbda0f69459a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "leetcode.h"

class MinStack {
public:
  stack<int> st;
  int min = INT_MAX;
  void push(int val) {
    if (min >= val) {
      st.push(min);
      min = val;
    }
    st.push(val);
  }
  void pop() {
    if (min == st.top()) {
      st.pop();
      min = st.top();
    }
    st.pop();
  }
  int top() { return st.top(); }
  int getMin() { return min; }
};