-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMin_Stack.java
More file actions
35 lines (35 loc) · 838 Bytes
/
Copy pathMin_Stack.java
File metadata and controls
35 lines (35 loc) · 838 Bytes
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
import java.util.*;
class MinStack{
Stack<Integer> st = new Stack<>();
Stack<Integer> min = new Stack<>();
public void push(int val){
if(st.size()==0){
st.push(val);
min.push(val);
}
else{
st.push(val);
if(min.peek()<val) min.push(min.peek());
else min.push(val);
}
}
public void pop(){
st.pop();
min.pop();
}
public int getMin(){
return min.peek();
}
}
public class Min_Stack {
public static void main(String[] args) {
MinStack m = new MinStack();
m.push(7);
m.push(8);
m.push(5);
m.push(6);
m.push(3);
System.out.println(m.st);
System.out.println("Min element : "+m.getMin());
}
}