← DiffPush

Implement Min Heap

Baseline Heaps · Learning O(log n) insert/remove, O(1) peek · O(n)

The ER Triage Board

An emergency room keeps a triage board where the most critical patient is always at the top. New arrivals slot in without breaking that rule, and discharging the top patient promotes exactly one successor. The board supports three moves: admit a patient, discharge the most critical one, and peek at who is next. Build the board so every operation stays logarithmic.

Input: A list of operations: ["insert", value], ["removeMin"], or ["minElement"].

Output: Return the list of values produced by every minElement operation, in order; a peek on an empty heap yields -1.

Constraints

Examples

Example 1

Input: {"operations":[["insert",5],["insert",3],["insert",8],["minElement"],["removeMin"],["minElement"]]}
Output: [3,5]
3 bubbles to the top; after discharging it, 5 becomes the new minimum.

Example 2

Input: {"operations":[["minElement"],["insert",-2],["minElement"]]}
Output: [-1,-2]
Peeking an empty board returns -1 before any patient arrives.

Solve this in your browser →

Also on LeetCode ↗