-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGetMin.java
46 lines (44 loc) · 1.13 KB
/
GetMin.java
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
44
45
46
package SummerTrainingGFG.Stack;
import java.util.Stack;
/*
* @author: Vishal Singh*/
public class GetMin{
static class StackGetMin{
Stack<Integer> main = new Stack<>();
Stack<Integer> aux = new Stack<>();
void push(int data){
if (main.isEmpty()) {
main.push(data);
aux.push(data);
System.out.println("Pushed "+data);
}else {
main.push(data);
System.out.println("Pushed "+data);
}
if (aux.peek() >= main.peek()){
aux.push(data);
}
}
void pop(){
if (main.peek().equals(aux.peek())) {
aux.pop();
}
System.out.println("Popped "+main.pop());
}
void getMin(){
System.out.println(aux.peek());
}
}
public static void main(String[] args) {
StackGetMin s = new StackGetMin();
s.push(3);
s.push(5);
s.getMin();
s.push(2);
s.push(1);
s.getMin();
s.pop();
s.getMin();
s.pop();
}
}