This content originally appeared on DEV Community and was authored by Tanuja V
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
int min = val;
if(!minStack.isEmpty() && minStack.peek()<min)
min = minStack.peek();
stack.push(val);
minStack.push(min);
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
Thanks for reading :)
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀
If you enjoy my content, support me by following me on my other socials:
https://linktr.ee/tanujav7
This content originally appeared on DEV Community and was authored by Tanuja V

Tanuja V | Sciencx (2024-07-21T03:35:29+00:00) Min Stack | LeetCode | Java. Retrieved from https://www.scien.cx/2024/07/21/min-stack-leetcode-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.