Approach
Breadth-first search
For Count Mentions Per User, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 56 lines of Java from the credited upstream file 3433.java.
- The implementation visibly relies on sequence storage, work queue.
- 6 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int[] countMentions(int numberOfUsers, List<List<String>> events) {3 record OfflineUser(int returnTimestamp, int userId) {}4 int[] ans = new int[numberOfUsers];5 boolean[] online = new boolean[numberOfUsers];6 Arrays.fill(online, true);7 8 Queue<OfflineUser> offlineQueue =9 new PriorityQueue<>(Comparator.comparingInt(OfflineUser::returnTimestamp));10 int allMentionsCount = 0;11 12 events.sort(13 Comparator.comparingInt((List<String> event) -> Integer.parseInt(event.get(1)))14 .thenComparing((List<String> event) -> event.get(0), Comparator.reverseOrder()));15 16 for (List<String> event : events) {17 final String eventType = event.get(0);18 final int timestamp = Integer.parseInt(event.get(1));19 20 while (!offlineQueue.isEmpty() && offlineQueue.peek().returnTimestamp <= timestamp)21 online[offlineQueue.poll().userId] = true;22 if (eventType.equals("MESSAGE")) {23 String mentionsString = event.get(2);24 if (mentionsString.equals("ALL")) {25 ++allMentionsCount;26 } else if (mentionsString.equals("HERE")) {27 for (int userId = 0; userId < numberOfUsers; ++userId)28 if (online[userId])29 ++ans[userId];30 } else {31 for (final int userId : getUserIds(mentionsString))32 ++ans[userId];33 }34 } else if (eventType.equals("OFFLINE")) {35 final int userId = Integer.parseInt(event.get(2));36 online[userId] = false;37 38 offlineQueue.offer(new OfflineUser(timestamp + 60, userId));39 }40 }41 42 43 for (int userId = 0; userId < numberOfUsers; ++userId)44 ans[userId] += allMentionsCount;45 46 return ans;47 }48 49 private List<Integer> getUserIds(final String s) {50 List<Integer> integers = new ArrayList<>();51 for (String part : s.split(" "))52 integers.add(Integer.parseInt(part.substring(2)));53 return integers;54 }55}56