Heap · Medium
An offline reader loads several already-sorted event feeds. It needs one sorted timeline without re-sorting all events.
Merge all integer feeds while retaining duplicate timestamps.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ feeds:integer[][] }
Input rules
- Each feed is sorted nondecreasing.
What each field means
feeds— integer arrays, each already sorted in nondecreasing order. Empty feeds are allowed.
Output
Return value and rules
- Return one nondecreasing array containing every input element.
- Preserve duplicate values, including duplicates from different feeds.
- No elements across all feeds produce
[].
Constraints
- 0 <= feeds.length <= 200; at most 100000 elements across all feeds
- Values in [-1000000000,1000000000]; empty feeds allowed.
Examples
Example 1
{
"feeds": [
[
1,
4
],
[
2,
3
],
[
0,
5
]
]
}Returns
[
0,
1,
2,
3,
4,
5
]Example 2
{
"feeds": []
}Returns
[]Required complexity
O(N log(k+1)) time and O(k) auxiliary space excluding output