forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinStack.java
89 lines (70 loc) · 2.47 KB
/
MinStack.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
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
86
87
88
89
// java function to design Hashset
// Time Complexity - O(1)
//Space Complexity - O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : earlier did not handle the case of duplicates but corrected it later
import java.util.*;
class MinStack {
List<Integer> stack;
List <Integer> minStack;
public MinStack() {
stack = new ArrayList<Integer>();
minStack = new ArrayList<Integer>();
}
public void push(int val) {
stack.add(val);// push value into the main stack
int newMin = val; // current minimum value
if(minStack.size() > 0){
newMin = Math.min(minStack.get(minStack.size() - 1),newMin);
} // compare the minimum value in stack with the last element in minStack
minStack.add(newMin); // smaller of the 2 is pushed into the minstack
}
public void pop() {
if(stack.size() != 0){
stack.remove(stack.size() - 1);
minStack.remove(minStack.size() - 1);
}
}
public int top() {
if(stack.size() != 0){
return stack.get(stack.size() - 1);
}
return 0;
}
public int getMin() {
if(minStack.size() != 0) {
return minStack.get(minStack.size() - 1);
}
return 0;
}
public static void main (String []args){
MinStack obj = new MinStack();
// Pushing elements
obj.push(5);
obj.push(2);
obj.push(2);
obj.push(1);
System.out.println("Top element: " + obj.top()); // Output: 1
System.out.println("Minimum element: " + obj.getMin()); // Output: 1
// Popping the top element
obj.pop();
System.out.println("Top element after pop: " + obj.top()); // Output: 2
System.out.println("Minimum element after pop: " + obj.getMin()); // Output: 2
// Pushing more elements
obj.push(0);
System.out.println("Top element: " + obj.top()); // Output: 0
System.out.println("Minimum element: " + obj.getMin()); // Output: 0
// Popping again
obj.pop();
System.out.println("Top element after pop: " + obj.top()); // Output: 2
System.out.println("Minimum element after pop: " + obj.getMin()); // Output: 2
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/