Replay an Operation Log on a Min-Heap

Implement replayMinHeap

You are given a log of operations to run against an initially empty min-heap: insert a value, extractMin (remove and report the smallest value), peekMin (report the smallest value without removing it) and size (report how many values are stored). Return the answer produced by every extractMin, peekMin and size operation, in order. Asking an empty heap for its minimum reports -1.

The heap can be implemented as a simple unsorted list, but then every minimum query is a full scan. The array-backed binary heap keeps the minimum at index 0 and repairs itself along a single root-to-leaf path after each change, so both inserting and extracting cost O(log n).

Example 1:

Input: ops = ["insert","insert","peekMin","extractMin","extractMin","extractMin"], values = [5,2,0,0,0,0]

Output: [2,2,5,-1]

Example 2:

Input: ops = ["insert","insert","insert","extractMin","extractMin","size"], values = [4,4,4,0,0,0]

Output: [4,4,1]

Example 3:

Input: ops = ["peekMin","extractMin","size"], values = [0,0,0]

Output: [-1,-1,0]

+ 8 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ ops.length ≤ 100, and values.length equals ops.length
  • ●Each op is "insert", "extractMin", "peekMin" or "size"; the matching values entry is only read for "insert"
  • ●0 ≤ values[i] ≤ 1000 for every "insert"
  • ●"extractMin" and "peekMin" on an empty heap report -1

ops =

["insert", "insert", "peekMin", "extractMin", "extractMin", "extractMin"]

values =

[5, 2, 0, 0, 0, 0]