Problem · Array
Pack Stream Chunks into Fixed-Capacity Nodes
Learn this problemProblem statement
A byte stream is delivered as a finite ordered sequence of chunks. You are given those chunks as chunks, where each integer is one byte value.
Pack the bytes, without reordering or dropping any value, into consecutive nodes of capacity 32. Fill every node greedily to length 32 before starting the next node. Only the final node may contain fewer than 32 bytes.
Return the linked-list payloads in order as a two-dimensional array, one row per node. If the stream ends before returning any bytes, return an empty array.
Function
packStreamChunks(chunks: int[][]) → int[][]Examples
Example 1
chunks = [[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49],[50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69]]return = [[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],[32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63],[64,65,66,67,68,69]]The two calls provide 70 bytes. Greedy capacity-32 packing produces node lengths [32,32,6], exactly as in the source.
Example 2
chunks = [[1,2],[3]]return = [[1,2,3]]All three bytes fit in the final partial node and preserve stream order.
Constraints
0 <= chunks.length <= 4.1 <= chunks[i].length.- The total number of bytes is at most
3000. 0 <= chunks[i][j] <= 255.