forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecialMinStack.java
More file actions
43 lines (34 loc) · 1.07 KB
/
SpecialMinStack.java
File metadata and controls
43 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.geeksforgeeks.stack;
import java.util.Stack;
public class SpecialMinStack extends Stack<Integer> {
Stack<Integer> minStack = new Stack<>();
public void push(int item) {
if (isEmpty()) {
super.push(item);
minStack.push(item);
} else {
super.push(item);
if (minStack.peek() > item) {
minStack.push(item);
} else {
minStack.push(minStack.peek());
}
}
}
public Integer pop() {
minStack.pop();
return super.pop();
}
public Integer getMin() {
return minStack.peek();
}
public static void main(String[] args) {
SpecialMinStack specialMinStack = new SpecialMinStack();
specialMinStack.push(10);
specialMinStack.push(20);
specialMinStack.push(30);
System.out.println("Minimum Element in the Stack is "+specialMinStack.getMin());
specialMinStack.push(5);
System.out.println("Minimum Element in the Stack is "+specialMinStack.getMin());
}
}