forked from diwu001/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxQueue.java
42 lines (36 loc) · 1.09 KB
/
MaxQueue.java
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
import java.util.LinkedList;
/*Implement a max Queue, which can get the maximum element in the queue in O(1) time.
Solution: use LinkedList to mimic a Two way queue. Another solution is use two max stacks to implement a max queue.*/
public class MaxQueue {
static LinkedList<Integer> data; // Queue<Integer> doesn't have getLast()
static LinkedList<Integer> max;
public MaxQueue () {
data = new LinkedList<Integer>();
max = new LinkedList<Integer>();
}
void add(int x) {
data.add(x);
while(!max.isEmpty() && max.getLast()<x) max.poll();
max.add(x);
}
void poll(){
if(max.isEmpty()) throw new NullPointerException("empty queue");
if(max.peek()== data.peek()) max.poll();
data.poll();
}
int max() {
if(max.isEmpty()) throw new NullPointerException("empty queue");
return max.peek();
}
public static void main(String[] args){
MaxQueue q = new MaxQueue();
q.add(5);
q.add(4);
q.add(6);
System.out.println(q.max());
q.poll(); //5
System.out.println(q.max());
q.poll(); //4
System.out.println(q.max());
}
}