← Back to problems
UNSW COMP2521 · medium

Priority Queue: insert / extract-max

Priority Queue: insert / extract-max

Implement a max-priority queue processing a command stream: I x (insert), X (extract and print the current max; never called on empty).

Input: line 1 m; then m command lines. Output: one line per X — the extracted maximum.

Example: I 5 / I 1 / I 9 / X / I 3 / X → 9 then 5. A heap gives O(log n) per op.