Problem · Hash Table
Ordered Payload Release
Learn this problemProblem statement
Payloads arrive one at a time. The ith arrival has positive sequence number sequenceNumbers[i] and string payload payloads[i]. Payloads must be released in increasing sequence-number order, beginning with sequence number 1.
Process the arrivals in their given order:
- If the arrival is ahead of the next expected sequence number, buffer it.
- After each arrival, immediately release every contiguous buffered payload beginning with the next expected sequence number.
- Release those payloads in increasing sequence-number order.
- If a sequence number has already been released, ignore a later arrival with that sequence number.
Return one row for every arrival. Row i contains exactly the payloads released immediately after arrival i.
Function
releasePayloadsInOrder(sequenceNumbers: int[], payloads: String[]) → String[][]Examples
Example 1
sequenceNumbers = [2,1,4,3,2,5]payloads = ["B","A","D","C","B","E"]return = [[],["A","B"],[],["C","D"],[],["E"]]Sequence 2 is buffered until sequence 1 arrives, so the second arrival releases A and B. The same pattern releases C and D when sequence 3 arrives. The later duplicate of sequence 2 is ignored because it was already released.