-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-716-Max-Stack.java
More file actions
150 lines (123 loc) · 3.83 KB
/
LeetCode-716-Max-Stack.java
File metadata and controls
150 lines (123 loc) · 3.83 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class MaxStack {
// 1. Using Two Stacks
/*
Runtime: 88 ms, faster than 30.97% of Java online submissions for Max Stack.
Memory Usage: 51.3 MB, less than 25.90% of Java online submissions for Max Stack.
*/
// class MaxStack {
// Stack<Integer> stack;
// Stack<Integer> maxStack;
// /** initialize your data structure here. */
// public MaxStack() {
// this.stack = new Stack<>();
// this.maxStack = new Stack<>();
// }
// public void push(int x) {
// stack.push(x);
// if (maxStack.isEmpty()) {
// maxStack.push(x);
// } else {
// maxStack.push(Math.max(x, maxStack.peek()));
// }
// }
// public int pop() {
// maxStack.pop();
// return stack.pop();
// }
// public int top() {
// return stack.peek();
// }
// public int peekMax() {
// return maxStack.peek();
// }
// public int popMax() {
// if (stack.peek() == maxStack.peek()) {
// stack.pop();
// return maxStack.pop();
// }
// int maxOnTop = maxStack.peek();
// Stack<Integer> temp = new Stack<>();
// while(stack.peek() != maxOnTop) {
// temp.push(this.pop());
// }
// stack.pop();
// maxStack.pop();
// while(!temp.isEmpty()) this.push(temp.pop());
// return maxOnTop;
// }
// }
// 2. Doubly-LinkedList + TreeMap
/*
https://leetcode.com/problems/max-stack/discuss/129922/Java-simple-solution-with-strict-O(logN)-push()popMax()pop()
Store values nodes in Doubly-LinkedList,
Using TreeMap as a <Sorted Value, List of Nodes> pair. As TreeMap could do GET/PUT/DELETE in O(logN), so
pop()/push()/popMax() Time O(logN)
<- (prev) curr (next) -> <- (prev) head (next) ->
*/
private class Node {
int val;
Node prev, next;
public Node (int val) {
this.val = val;
}
}
private Node head;
private TreeMap<Integer, LinkedList<Node>> map;
/** initialize your data structure here. */
public MaxStack() {
head = new Node(-1);
map = new TreeMap<>();
}
public void push(int x) {
Node curr = new Node(x);
addToHead(curr);
map.computeIfAbsent(x, k -> new LinkedList<>()).add(curr);
}
public int pop() {
Node curr = head.prev;
if (curr == null) return -1; // no element exist
deleteNode(curr);
// since it's pop(), we are always sure that the last element in the map's value list will be the tail
map.get(curr.val).removeLast();
if (map.get(curr.val).isEmpty()) {
map.remove(curr.val);
}
return curr.val;
}
public int top() {
return head.prev.val;
}
public int peekMax() {
return map.lastKey();
}
public int popMax() {
int maxVal = peekMax();
Node max = map.get(maxVal).removeLast();
deleteNode(max);
if (map.get(maxVal).isEmpty()) {
map.remove(maxVal);
}
return maxVal;
}
private void addToHead(Node curr) {
curr.next = head;
curr.prev = head.prev;
if (head.prev != null) head.prev.next = curr;
head.prev = curr;
}
private void deleteNode(Node curr) {
if (curr.prev != null) curr.prev.next = curr.next;
if (curr.next != null) curr.next.prev = curr.prev;
curr.next = null;
curr.prev = null;
}
}
/**
* Your MaxStack object will be instantiated and called as such:
* MaxStack obj = new MaxStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.peekMax();
* int param_5 = obj.popMax();
*/