Numbers Station Assembly
EasyStreams · Optiver
A receiver listens to a "numbers station" that broadcasts a message split into numbered fragments. Fragment numbers start at 1 and go up by one, but fragments arrive out of order, and some are retransmitted, so the same fragment can arrive more than once.
packets lists the fragments in arrival order; each is [sequence_number, payload]. A retransmitted fragment always has the same payload as the original.
The receiver may only print text in order: after a fragment arrives, it prints every fragment that is now ready, i.e. the longest run of consecutive, not-yet-printed fragments starting at the next number it is waiting for. Duplicates of fragments that are already buffered or already printed are ignored.
Return a list with one string per packet: the text printed right after that packet arrived (the ready payloads concatenated in sequence order), or "" if nothing became printable.
Example 1
- Input:
- packets = [[2, "SEVEN "], [3, "FOUR "], [1, "NINE "], [5, "ZERO"], [4, "ONE "]]
- Output:
- ["", "", "NINE SEVEN FOUR ", "", "ONE ZERO"]
- Explanation
- Fragments 2 and 3 wait because 1 is missing. When 1 arrives, 1, 2 and 3 print together. Fragment 5 waits for 4; when 4 arrives, both print.
Example 2
- Input:
- packets = [[1, "ab"], [1, "ab"], [3, "ef"], [3, "ef"], [2, "cd"]]
- Output:
- ["ab", "", "", "", "cdef"]
- Explanation
- The repeat of fragment 1 arrives after it was printed, and the repeat of 3 arrives while 3 is still buffered; both are ignored. Fragment 2 then releases
"cd"and the buffered"ef".
Example 3
- Input:
- packets = [[4, "d"], [2, "b"], [3, "c"]]
- Output:
- ["", "", ""]
- Explanation
- Fragment 1 never arrives, so nothing can be printed and every entry is an empty string.
Constraints
- 1 ≤ packets.length ≤ 105
- 1 ≤ sequence_number ≤ 109
- Payloads are strings (possibly empty).
- Not every sequence number has to arrive; printing simply stops at the first gap.