AlgoFrames

Trace a size-k min-heap

Standard · Trace the algorithm · about 6 min

Keep the 3 largest values of [4, 9, 2, 7, 5, 8] with a min-heap of size 3. Answer each question before the step plays.

Example 1

Input:

{"values":[4,9,2,7,5,8],"k":3}

Output:

[9,8,7]

Constraints

  • The trace uses the input shown.

Learn the idea first: Priority queues in practice: top-k, k-way merge, scheduling, a visual lesson.

More Priority queues in practice: top-k, k-way merge, scheduling problems