- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 53 lines of Python from the credited upstream file 3433.py.
- The implementation visibly relies on sequence storage, work queue.
- No explicit loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from dataclasses import dataclass2 3 4@dataclass(frozen=True)5class OfflineUser:6 returnTimestamp: int7 userId: int8 9 def __lt__(self, other):10 return self.returnTimestamp < other.returnTimestamp11 12 13class Solution:14 def countMentions(15 self,16 numberOfUsers: int,17 events: list[list[str]]18 ) -> list[int]:19 ans = [0] * numberOfUsers20 online = [True] * numberOfUsers21 offlineQueue = [] 22 allMentionsCount = 023 24 events.sort(key=lambda x: (int(x[1]), -ord(x[0][0])))25 26 for eventType, t, messageContent in events:27 timestamp = int(t)28 29 while offlineQueue and offlineQueue[0].returnTimestamp <= timestamp:30 user = heapq.heappop(offlineQueue)31 online[user.userId] = True32 if eventType == "MESSAGE":33 match messageContent:34 case "ALL":35 allMentionsCount += 136 case "HERE":37 for userId in range(numberOfUsers):38 if online[userId]:39 ans[userId] += 140 case _:41 for userId in [int(part[2:]) for part in messageContent.split()]:42 ans[userId] += 143 elif eventType == "OFFLINE":44 userId = int(messageContent)45 online[userId] = False46 47 heapq.heappush(offlineQueue, OfflineUser(timestamp + 60, userId))48 49 50 for userId in range(numberOfUsers):51 ans[userId] += allMentionsCount52 return ans53