Problem
Write a solution function that processes a series of queries using a priority queue and returns the results.
Query Information
x(xis a natural number):
Insertxinto the priority queue.
No changes are made to the result.0:
Remove the smallest number from the priority queue and add it to the result. If the priority queue is empty, add0to the result instead.
Concept Explanation
A standard queue operates on a First-In-First-Out (FIFO) principle.
Refer to "DailyAlgo: Queue".
However, a priority queue outputs elements based on their priority, not their insertion order.
A priority queue supports the following operations:
push X: Insert elementXinto the queue.pop: Remove and return the element with the highest priority.
To define a priority queue, you must specify the elements and the criteria for their priority.
For example, consider a priority queue of natural numbers where smaller numbers have higher priority:
(push 4), (push 10), (push 7), (pop), (pop), (push 5), (pop), (push 15), (pop)
If implemented as a descending-sorted array, the operations can be visualized as follows:
- On
push, insert the element and sort the array in descending order. The smallest number (highest priority) is placed at the end. - On
pop, remove the last element from the array.

With this approach:
pushrequires sorting, resulting in a time complexity of .popinvolves removing the last element, with a time complexity of .
Thus, for queries, the total time complexity is .
Using a heap to implement the priority queue reduces the overall complexity to .
Heap Definition
- A heap is a complete binary tree.
- Every node is greater than or equal to its children in priority.
The following examples are not heaps:
- The first does not maintain a complete binary tree structure.
- The second violates the priority rule.


Consider the following valid heap:

Push Operation
To push 13:
- Add
13as the last node to maintain the complete binary tree structure. - Swap it with its parent if it has a higher priority.
- Repeat until the root or a parent with higher priority is reached.


Pop Operation
To pop:
- Remove the root node (highest priority).
- Replace it with the last node.
- Swap with the child of lower priority until reaching a leaf or maintaining heap properties.



Using a heap, both push and pop operations have a time complexity of , and the total complexity for queries is O(N log N)
Constraints
- Each element in
queriesis an integer between 0 and 10,000 inclusive. - The length of
queriesis at most 100,000. queriescontains at least one0.
Test Cases
| Test Case Number | queries | return |
|---|---|---|
| 1 | [4,10,7,0,0,5,0,15,0] | [4,7,5,10] |
| 2 | [5,5,5,0,3,0,0] | [5,3,5] |
| 3 | [0,1,0,0] | [0,1,0] |
Explanation for Test Case 1
- Same as the example provided above.
Explanation for Test Case 2
| Query | Priority Queue | Result |
|---|---|---|
| push 5 | [5] | [] |
| push 5 | [5, 5] | [] |
| push 5 | [5, 5, 5] | [] |
| pop | [5, 5] | [5] |
| push 3 | [5, 5, 3] | [5] |
| pop | [5, 5] | [5, 3] |
| pop | [5] | [5, 3, 5] |
Explanation for Test Case 3
| Query | Priority Queue | Result |
|---|---|---|
| pop | [] | [0] |
| push 1 | [1] | [0] |
| pop | [] | [0, 1] |
| pop | [] | [0, 1, 0] |