Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- public class StackWithMin extends Stack<Integer> {
- Stack<Integer> s_min = new Stack<Integer>();
- public void push(int data) {
- if (data <= this.min()) {
- s_min.push(data);
- }
- super.push(data);
- }
- public Integer pop() {
- int value = super.pop();
- if (value == min()) {
- s_min.pop();
- }
- return value;
- }
- public int min() {
- if(s_min.isEmpty()){
- return Integer.MAX_VALUE;
- }
- return s_min.peek();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement