Hard · Multiway Merge
Understand → Visualise → Practice.
The next output must be the smallest unconsumed front value. A heap compares those k frontiers without scanning all arrays every time.
Push the first item of each non-empty array; after popping one, push its successor from the same array.
Push one entry per non-empty array.
Append it to the output.
Push only the next item from that array.
O(N log k) time and O(k) heap space for N total values.
Store the source array and index with each heap value so its successor can be found.
Read the full guide first: Heaps and Priority Queues.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.