aboutsummaryrefslogtreecommitdiff
path: root/min_stack.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'min_stack.cpp')
-rw-r--r--min_stack.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/min_stack.cpp b/min_stack.cpp
new file mode 100644
index 0000000..745c537
--- /dev/null
+++ b/min_stack.cpp
@@ -0,0 +1,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; }
+};