forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-stack.java
More file actions
46 lines (38 loc) · 1.32 KB
/
min-stack.java
File metadata and controls
46 lines (38 loc) · 1.32 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
// Time Complexity : O(1) for push, pop, top, and getMin
// Space Complexity : O(n) due to stack storage (extra space used to store previous minimums)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No.
// Approach:
// Use a single stack but store previous minimum values whenever the current minimum changes or remains same.
// When pushing a new minimum, first push the old minimum, then update min and push the new value.
// During pop, if the popped value is equal to current min, pop again to restore the previous minimum.
class MinStack {
private Stack<Integer> stack;
private int min;
public MinStack() {
this.stack = new Stack<>();
this.min = Integer.MAX_VALUE;
}
public void push(int val) {
// If new value is less than or equal to current min,
// store the old min before updating
if (val <= min) {
stack.push(min);
min = val;
}
stack.push(val);
}
public void pop() {
// If popped value is the current minimum,
// restore the previous minimum from stack
if (min == stack.pop()) {
min = stack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}