Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Many Rivers

Learn
Play
Prove
Problem context and objectives
Mission briefing

Matchmaking coordinator · Urgent players and jobs compete for limited slots

Keep the best next candidate available without sorting everything again: The Many Rivers.

A stale priority frontier increases wait time for everyone.

How you win

  1. 1Recognize when K-way heap merge matches the clues
  2. 2Keep this true after every move: the heap contains the smallest unmerged value from every nonempty list
  3. 3Reach the result within O(N log k)

Rules and pressure

  • Target cost: O(N log k)
  • State rule: the heap contains the smallest unmerged value from every nonempty list
Lesson 1 of 3

Live algorithm trace

K-way heap merge

Complete execution
1 of 6
1
root
4
5
1
3
4
2
6

Push one front from each of the three sorted lists.

1push the first value of every list
2pop the smallest front
3append it to output
4push the next value from its list
5repeat until heap is empty
6return output
heap = 1A,1B,2Coutput = []lists = [1,4,5] [1,3,4] [2,6]
Truth to preserve / Cost target
Truth to preserve

the heap contains the smallest unmerged value from every nonempty list

Cost target

O(N log k)

Each sorted list exposes one next candidate. A min-heap compares those k fronts, emits the smallest, then advances only the list that supplied it.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead