Skip to content

Commit b1f90b7

Browse files
refactor 716
1 parent 8748a21 commit b1f90b7

File tree

1 file changed

+104
-125
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+104
-125
lines changed

src/main/java/com/fishercoder/solutions/_716.java

+104-125
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,13 @@
66
import java.util.Stack;
77
import java.util.TreeMap;
88

9-
/**
10-
* 716. Max Stack
11-
*
12-
* Design a max stack that supports push, pop, top, peekMax and popMax.
13-
*
14-
push(x) -- Push element x onto stack.
15-
pop() -- Remove the element on top of the stack and return it.
16-
top() -- Get the element on the top.
17-
peekMax() -- Retrieve the maximum element in the stack.
18-
popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.
19-
20-
Example 1:
21-
MaxStack stack = new MaxStack();
22-
stack.push(5);
23-
stack.push(1);
24-
stack.push(5);
25-
stack.top(); -> 5
26-
stack.popMax(); -> 5
27-
stack.top(); -> 1
28-
stack.peekMax(); -> 5
29-
stack.pop(); -> 1
30-
stack.top(); -> 5
31-
Note:
32-
-1e7 <= x <= 1e7
33-
Number of operations won't exceed 10000.
34-
The last four operations won't be called when stack is empty.
35-
*/
369
public class _716 {
3710
public static class Solution1 {
38-
/**This is O(n) for popMax() and pop() while O(1) for the other three operations which is UN-acceptable during an interview!
39-
* We need to do better than O(n) time complexity in order to ace the interview!
40-
* But O(1) is impossible, so let's aim for O(logn).*/
11+
/**
12+
* This is O(n) for popMax() and pop() while O(1) for the other three operations which is UN-acceptable during an interview!
13+
* We need to do better than O(n) time complexity in order to ace the interview!
14+
* But O(1) is impossible, so let's aim for O(logn).
15+
*/
4116
public static class MaxStack {
4217

4318
private int max;
@@ -110,106 +85,110 @@ public int popMax() {
11085
}
11186
}
11287

113-
public static class Solution2 {
114-
/** Use a treemap and a doubly linked list to achieve O(logn) time complexity. */
88+
public static class Solution2 {
89+
/**
90+
* Use a treemap and a doubly linked list to achieve O(logn) time complexity.
91+
*/
11592

116-
static class Node {
117-
int val;
118-
Node prev;
119-
Node next;
93+
static class Node {
94+
int val;
95+
Node prev;
96+
Node next;
12097

121-
public Node(int val) {
122-
this.val = val;
123-
}
124-
}
98+
public Node(int val) {
99+
this.val = val;
100+
}
101+
}
125102

126-
static class DoublyLinkedList {
127-
Node head;
128-
Node tail;
129-
130-
public DoublyLinkedList() {
131-
head = new Node(0);
132-
tail = new Node(0);
133-
head.next = tail;
134-
tail.prev = head;
135-
}
136-
137-
public Node add(int val) {
138-
/**For this doubly linked list, we always add it to the end of the list*/
139-
Node x = new Node(val);
140-
x.next = tail;
141-
x.prev = tail.prev;
142-
tail.prev.next = x;
143-
tail.prev = tail.prev.next;
144-
return x;
145-
}
146-
147-
public int pop() {
148-
/**for pop(), we always pop one from the tail of the doubly linked list*/
149-
return unlink(tail.prev).val;
150-
}
151-
152-
public Node unlink(Node node) {
153-
node.prev.next = node.next;
154-
node.next.prev = node.prev;
155-
return node;
156-
}
157-
158-
public int peek() {
159-
return tail.prev.val;
160-
}
161-
}
103+
static class DoublyLinkedList {
104+
Node head;
105+
Node tail;
162106

163-
public static class MaxStack {
164-
TreeMap<Integer, List<Node>> treeMap;
165-
/**
166-
* the reason we have a list of nodes as treemap's value is because one value could be pushed
167-
* multiple times into this MaxStack and we want to keep track of all of them.
168-
*/
169-
DoublyLinkedList doublyLinkedList;
170-
171-
/** initialize your data structure here. */
172-
public MaxStack() {
173-
treeMap = new TreeMap();
174-
doublyLinkedList = new DoublyLinkedList();
175-
}
176-
177-
public void push(int x) {
178-
Node node = doublyLinkedList.add(x);
179-
if (!treeMap.containsKey(x)) {
180-
treeMap.put(x, new ArrayList<>());
181-
}
182-
treeMap.get(x).add(node);
183-
}
184-
185-
public int pop() {
186-
int val = doublyLinkedList.pop();
187-
List<Node> nodes = treeMap.get(val);
188-
nodes.remove(nodes.size() - 1);
189-
if (nodes.isEmpty()) {
190-
treeMap.remove(val);
107+
public DoublyLinkedList() {
108+
head = new Node(0);
109+
tail = new Node(0);
110+
head.next = tail;
111+
tail.prev = head;
112+
}
113+
114+
public Node add(int val) {
115+
/**For this doubly linked list, we always add it to the end of the list*/
116+
Node x = new Node(val);
117+
x.next = tail;
118+
x.prev = tail.prev;
119+
tail.prev.next = x;
120+
tail.prev = tail.prev.next;
121+
return x;
122+
}
123+
124+
public int pop() {
125+
/**for pop(), we always pop one from the tail of the doubly linked list*/
126+
return unlink(tail.prev).val;
127+
}
128+
129+
public Node unlink(Node node) {
130+
node.prev.next = node.next;
131+
node.next.prev = node.prev;
132+
return node;
133+
}
134+
135+
public int peek() {
136+
return tail.prev.val;
137+
}
191138
}
192-
return val;
193-
}
194-
195-
public int top() {
196-
return doublyLinkedList.peek();
197-
}
198-
199-
public int peekMax() {
200-
return treeMap.lastKey();
201-
}
202-
203-
public int popMax() {
204-
int max = treeMap.lastKey();
205-
List<Node> nodes = treeMap.get(max);
206-
Node node = nodes.remove(nodes.size() - 1);
207-
doublyLinkedList.unlink(node);
208-
if (nodes.isEmpty()) {
209-
treeMap.remove(max);
139+
140+
public static class MaxStack {
141+
TreeMap<Integer, List<Node>> treeMap;
142+
/**
143+
* the reason we have a list of nodes as treemap's value is because one value could be pushed
144+
* multiple times into this MaxStack and we want to keep track of all of them.
145+
*/
146+
DoublyLinkedList doublyLinkedList;
147+
148+
/**
149+
* initialize your data structure here.
150+
*/
151+
public MaxStack() {
152+
treeMap = new TreeMap();
153+
doublyLinkedList = new DoublyLinkedList();
154+
}
155+
156+
public void push(int x) {
157+
Node node = doublyLinkedList.add(x);
158+
if (!treeMap.containsKey(x)) {
159+
treeMap.put(x, new ArrayList<>());
160+
}
161+
treeMap.get(x).add(node);
162+
}
163+
164+
public int pop() {
165+
int val = doublyLinkedList.pop();
166+
List<Node> nodes = treeMap.get(val);
167+
nodes.remove(nodes.size() - 1);
168+
if (nodes.isEmpty()) {
169+
treeMap.remove(val);
170+
}
171+
return val;
172+
}
173+
174+
public int top() {
175+
return doublyLinkedList.peek();
176+
}
177+
178+
public int peekMax() {
179+
return treeMap.lastKey();
180+
}
181+
182+
public int popMax() {
183+
int max = treeMap.lastKey();
184+
List<Node> nodes = treeMap.get(max);
185+
Node node = nodes.remove(nodes.size() - 1);
186+
doublyLinkedList.unlink(node);
187+
if (nodes.isEmpty()) {
188+
treeMap.remove(max);
189+
}
190+
return max;
191+
}
210192
}
211-
return max;
212-
}
213193
}
214-
}
215194
}

0 commit comments

Comments
 (0)