-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path65.java
More file actions
33 lines (29 loc) · 744 Bytes
/
65.java
File metadata and controls
33 lines (29 loc) · 744 Bytes
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
class MaxQueue {
LinkedList<Integer> list1;
LinkedList<Integer> list2;
public MaxQueue() {
list1 = new LinkedList<>();
list2 = new LinkedList<>();
}
public int max_value() {
if(!list2.isEmpty()){
return list2.peekFirst();
}
return -1;
}
public void push_back(int value) {
list1.add(value);
while(!list2.isEmpty() && value > list2.peekLast()){
list2.removeLast();
}
list2.addLast(value);
}
public int pop_front() {
if(!list1.isEmpty()){
int value = list1.poll();
if(value == list2.peekFirst()) list2.removeFirst();
return value;
}
return -1;
}
}