-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathMinStack.java
More file actions
94 lines (77 loc) · 2.01 KB
/
MinStack.java
File metadata and controls
94 lines (77 loc) · 2.01 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
86
87
88
89
90
91
92
93
94
import java.util.Stack;
/*
Leetcode 115
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
*/
public class MinStack115 {
/*
Using 2 stack
space complexity o(n)+o(n) -- o(n)
Time complexity for push,pop,top and min is o(1)
1st stack to maintain the insertion order
2nd stack to maintain the corresponding min element until current push
*/
Stack<Integer> minStack;
Stack<Integer> stack;
public MinStack() {
minStack = new Stack<>();
stack = new Stack<>();
}
public void push(int val) {
if(stack.isEmpty()){
minStack.push(val);
}else{
if(val < minStack.peek())
minStack.push(val);
else
minStack.push(minStack.peek());
}
stack.push(val);
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
/*
Using SinglyLinkedList
space complexity o(n)
Time complexity for push,pop,top and min is o(1)
Maintain min element in the node (every new node insert should compare the minimum element
of previous node which is the minimum of all inserted element)
*/
Node head;
public MinStack() {
head = null;
}
public void push(int val) {
if(head == null)
head = new Node(val,val, null);
else
head = new Node(val, Math.min(val, head.min), head);
}
public void pop() {
head = head.next;
}
public int top() {
return head.val;
}
public int getMin() {
return head.min;
}
}
class Node{
public int val;
public int min;
public Node next;
public Node(int val, int min, Node next){
this.val=val;
this.next=next;
this.min=min;
}
}