forked from yuanguangxin/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b762c3
commit 38f972b
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |