Problem · Array
Track Received Byte Ranges
Learn this problemProblem statement
A file is uploaded as a sequence of byte-range chunks. Each chunks[i] is an inclusive, 1-based range [start, end].
After each chunk arrives, consolidate every byte received so far into disjoint consecutive ranges. Overlapping or adjacent ranges merge. For example, [1, 3] and [4, 6] merge because 4 <= 3 + 1.
Return one snapshot after each arrival. Within each snapshot, sort ranges by their left endpoint and serialize each range as start:end.
Function
trackReceivedByteRanges(chunks: long[][]) → String[][]Examples
Example 1
chunks = [[1,3],[4,6]]return = [["1:3"],["1:6"]]After the first arrival the received range is 1:3. The second chunk is adjacent, so the ranges merge into 1:6.
Example 2
chunks = [[8,10],[1,2],[2,8],[4,5]]return = [["8:10"],["1:2","8:10"],["1:10"],["1:10"]]The third chunk connects and overlaps both existing ranges, producing 1:10. The final contained chunk leaves that range unchanged.
Constraints
1 <= chunks.length <= 2000.- Every
chunks[i]contains exactly two signed 64-bit integers. 1 <= chunks[i][0] <= chunks[i][1] <= 10^18.- Duplicate and fully contained chunks are valid and do not change the consolidated ranges.