You are given a list of messages from a queue. Each message is [content, timestamp], where content is a string and timestamp is a time in seconds. Discard a message if a duplicate (a message with the same content) was accepted within the last 60 seconds.
A message is a duplicate if another message with the same content was accepted within the previous 60 seconds, that is, timestamp - last_accepted_timestamp <= 60. Messages are given in chronological order (non-decreasing timestamps).
Return the list of accepted messages in their original order.
Function Signature
def filter_messages(messages: list[list]) -> list[list]:
Rules
-
Each message is a two-element list
[content, timestamp]
with a string and an integer. Each accepted message appears in the output unchanged, as the same
[content, timestamp]
pair.
-
The window is measured from the last accepted message with the same content. A discarded message does not reset the window.
-
A gap of exactly 60 seconds still counts as a duplicate. The message is accepted only when the gap is more than 60.
-
Contents are compared exactly, character for character, including spaces.
Constraints
-
1 <= messages.length <= 10^5
-
1 <= len(content) <= 100
-
0 <= timestamp <= 10^9
-
Timestamps are in non-decreasing order.
-
Message content consists of lowercase English letters and spaces.
Examples
Input: messages = [["hello",1],["world",2],["hello",50],["hello",62],["world",63]]
Output: [["hello",1],["world",2],["hello",62],["world",63]]
The first "hello" and the first "world" are accepted. "hello" at 50 is discarded because 50 - 1 = 49 is at most 60. "hello" at 62 is compared with the accepted one at 1 (the discarded one at 50 does not count), and 62 - 1 = 61 is more than 60, so it is accepted; "world" at 63 is accepted for the same reason (63 - 2 = 61).
Input: messages = [["ping",100],["pong",100],["ping",130],["ping",160],["pong",161],["ping",191]]
Output: [["ping",100],["pong",100],["pong",161],["ping",191]]
"ping" at 130 and at 160 are discarded because the gaps to the accepted "ping" at 100 are 30 and 60. "pong" at 161 is accepted (161 - 100 = 61) and "ping" at 191 is accepted (191 - 100 = 91), and the output keeps the input order.