Timed Message Batches
Problem statement
A messaging service collects incoming messages into a batch. When a message arrives and no batch is open, it opens a new batch and starts a timer that fires window time units later. Every message that arrives before the timer fires joins that batch. When the timer fires, the service logs the batch and clears it. A message that arrives at exactly the firing time goes into a new batch.
Message i arrives at time times[i]. Return the logged batches in the order they are logged, each batch listing its messages in arrival order. Every batch is eventually logged, including the last one.
Function
timedBatches(times: int[], messages: String[], window: int) → List<List<String>>Examples
Example 1
times = [3,6,9,14]messages = ["ping","pong","ack","done"]window = 5return = [["ping","pong"],["ack"],["done"]]The first message at time 3 opens a batch that is logged at 3 + 5 = 8, so it holds ping and pong. ack at 9 opens the next batch, logged at 14. done arrives at 14, exactly when that batch is logged, so it starts a third batch.
Example 2
times = [0,4,4,8]messages = ["a1","b2","c3","d4"]window = 4return = [["a1"],["b2","c3"],["d4"]]The batch opened at time 0 is logged at 4, before the two messages that arrive at 4, so it holds only a1. Those two open the next batch, logged at 8, and d4 at 8 starts a new one.
Example 3
times = [100]messages = ["solo"]window = 1return = [["solo"]]One message, one batch, logged at time 101.
Constraints
1 <= times.length <= 2000andmessages.length == times.length0 <= times[i] <= 10^9, andtimesnever decreases- each message is 1 to 10 characters, lowercase letters or digits
1 <= window <= 10^9