Leetcode 155 Solution
This article provides solution to leetcode question 155 (min-stack)
Access this page by simply typing in "lcs 155" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/min-stack
Solution
class MinStack {
stack<int> a;
stack<int> b;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
int min_val;
if (b.empty() == false)
min_val = min(x, b.top());
else
min_val = x;
a.push(x);
b.push(min_val);
}
void pop() {
a.pop();
b.pop();
}
int top() {
return a.top();
}
int getMin() {
return b.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/