forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
47 lines (40 loc) · 1.31 KB
/
MinStack.java
File metadata and controls
47 lines (40 loc) · 1.31 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
// Time Complexity : O(1) for push, pop, top and getMin
// Space Complexity : O(n) where n is the number of elements in the stack
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
// This approach uses two stacks:
// a primary stack to store all the elements and
// a min stack to keep track of the minimum element at each level of the primary stack.
// Whenever a new element is pushed onto the primary stack,
// we compare it with the current minimum element (the top of the min stack) and
// push the smaller of the two onto the min stack.
import java.util.Stack;
class MinStack {
Stack<Integer> primaryStack;
Stack<Integer> minStack;
public MinStack() {
primaryStack = new Stack<Integer>();
minStack = new Stack<Integer>();
}
public void push(int val) {
primaryStack.push(val);
if(minStack.isEmpty() || val < minStack.peek()){
minStack.push(val);
} else {
minStack.push(minStack.peek());
}
}
public void pop() {
if(primaryStack.isEmpty()){
return;
}
primaryStack.pop();
minStack.pop();
}
public int top() {
return primaryStack.peek();
}
public int getMin() {
return minStack.peek();
}
}