Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,37 @@
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach
import java.util.Stack;

class MinStack {
Stack<Integer> mainStack;
Stack<Integer> minStack;

public MinStack() {
mainStack = new Stack<>();
minStack = new Stack<>();
}

public void push(int val) {
mainStack.push(val);
if (minStack.isEmpty() || val <= minStack.peek())
minStack.push(val);
}

public void pop() {
int poppedValue = mainStack.pop();
if (poppedValue == minStack.peek())
minStack.pop();
}

public int top() {
return mainStack.peek();
}

public int getMin() {
return minStack.peek();
}
}