-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtenth.java
More file actions
85 lines (63 loc) · 1.99 KB
/
Copy pathtenth.java
File metadata and controls
85 lines (63 loc) · 1.99 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.*;
public class tenth {
private Stack<Integer> stack;
private Stack<Integer> Minstack;
public tenth(){
stack=new Stack<>();
Minstack=new Stack<>();
}
public void push(int x){
stack.push(x);
if(Minstack.isEmpty()||x<=Minstack.peek()){
Minstack.push(x);
}
}
public void pop(){
int popedvalue=stack.pop();
if(popedvalue==Minstack.peek()){
Minstack.pop();
}
}
public int top(){
return stack.peek();
}
public int getMin(){
return Minstack.peek();
}
public static void main(String[] args){
tenth minstack=new tenth();
minstack.push(-2);
minstack.push(0);
minstack.push(-3);
System.out.println("Minimum: " +minstack.getMin()); // -3
minstack.pop();
System.out.println("Top: " +minstack.top()); // 0
System.out.println("Minimum: " +minstack.getMin()); // -2
System.out.println("\n========== Test Case 2 ==========");
tenth minstack2 = new tenth();
minstack2.push(5);
minstack2.push(2);
minstack2.push(10);
minstack2.push(1);
System.out.println("Minimum: " + minstack2.getMin()); // 1
minstack2.pop();
System.out.println("Minimum after pop: " + minstack2.getMin()); // 2
minstack2.pop();
System.out.println("Top: " + minstack2.top()); // 2
System.out.println("Minimum: " + minstack2.getMin()); // 2
System.out.println("\n========== Test Case 3 (Duplicate Minimums) ==========");
tenth minstack3 = new tenth();
minstack3.push(3);
minstack3.push(2);
minstack3.push(2);
minstack3.push(1);
minstack3.push(1);
System.out.println("Minimum: " + minstack3.getMin()); // 1
minstack3.pop();
System.out.println("Minimum: " + minstack3.getMin()); // 1
minstack3.pop();
System.out.println("Minimum: " + minstack3.getMin()); // 2
minstack3.pop();
System.out.println("Minimum: " + minstack3.getMin()); // 2
}
}