Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanguangxin committed Jul 3, 2020
1 parent 4b762c3 commit 38f972b
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package 栈相关.q232_用栈实现队列.含有最大值的队列;

import java.util.LinkedList;
import java.util.Queue;

public class MaxQueue {

private Queue<Integer> queue;
private LinkedList<Integer> max;

public MaxQueue() {
queue = new LinkedList<>();
max = new LinkedList<>();
}

public int max_value() {
return max.size() == 0 ? -1 : max.getFirst();
}

public void push_back(int value) {
queue.add(value);
while (max.size() != 0 && max.getLast() < value) {
max.removeLast();
}
max.add(value);
}

public int pop_front() {
if (max.size() != 0 && queue.peek().equals(max.getFirst())) {
max.removeFirst();
}
return queue.size() == 0 ? -1 : queue.poll();
}
}

0 comments on commit 38f972b

Please sign in to comment.