-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
46 lines (39 loc) · 1.3 KB
/
Copy pathsolution.java
File metadata and controls
46 lines (39 loc) · 1.3 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
// Java implementation
import java.util.*;
class SpecialQueue {
private Queue<Integer> q; // FIFO queue
private Deque<Integer> minD; // increasing deque for minima
private Deque<Integer> maxD; // decreasing deque for maxima
public SpecialQueue() {
q = new LinkedList<>();
minD = new ArrayDeque<>();
maxD = new ArrayDeque<>();
}
// Insert element x at rear
public void enqueue(int x) {
q.offer(x);
while (!minD.isEmpty() && minD.peekLast() > x) minD.pollLast();
minD.offerLast(x);
while (!maxD.isEmpty() && maxD.peekLast() < x) maxD.pollLast();
maxD.offerLast(x);
}
// Remove element from front
public void dequeue() {
if (q.isEmpty()) return;
int v = q.poll();
if (!minD.isEmpty() && minD.peekFirst() == v) minD.pollFirst();
if (!maxD.isEmpty() && maxD.peekFirst() == v) maxD.pollFirst();
}
// Get front element
public int getFront() {
return q.isEmpty() ? -1 : q.peek();
}
// Get minimum element
public int getMin() {
return minD.isEmpty() ? -1 : minD.peekFirst();
}
// Get maximum element
public int getMax() {
return maxD.isEmpty() ? -1 : maxD.peekFirst();
}
}