|
| 1 | +func countMentions(numberOfUsers int, events [][]string) []int { |
| 2 | + ans := make([]int, numberOfUsers) |
| 3 | + // Event struct: timestamp, type (1=offline, -1=online, 2=HERE mention), user id |
| 4 | + type event struct { |
| 5 | + timestamp, type_, id int |
| 6 | + } |
| 7 | + es := []event{} |
| 8 | + all := 0 |
| 9 | + // Process raw events and separate them into individual event objects |
| 10 | + for _, e := range events { |
| 11 | + curT, _ := strconv.Atoi(e[1]) |
| 12 | + mention := e[2] |
| 13 | + if e[0] == "OFFLINE" { |
| 14 | + // Create offline(1) and online(-1) events 60 seconds later |
| 15 | + i, _ := strconv.Atoi(mention) |
| 16 | + es = append(es, event{curT, 1, i}, event{curT + 60, -1, i}) |
| 17 | + } else if mention == "ALL" { |
| 18 | + all++ |
| 19 | + } else if mention == "HERE" { |
| 20 | + // Increment all user mentions for HERE events (process later) |
| 21 | + all++ |
| 22 | + es = append(es, event{curT, 2, -1}) |
| 23 | + } else { |
| 24 | + // Direct user mentions: add immediately |
| 25 | + for _, s := range strings.Split(mention, " ") { |
| 26 | + i, _ := strconv.Atoi(s[2:]) |
| 27 | + ans[i]++ |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Sort events by timestamp, then by type (process in correct order) |
| 33 | + slices.SortFunc(es, func(a, b event) int { |
| 34 | + return cmp.Or(a.timestamp-b.timestamp, a.type_-b.type_) |
| 35 | + }) |
| 36 | + |
| 37 | + // Calculate HERE mentions: count online users at each HERE event |
| 38 | + here := 0 |
| 39 | + for _, e := range es { |
| 40 | + if e.type_ == 2 { |
| 41 | + // HERE mention: increment all currently online users by 'here' count |
| 42 | + here++ |
| 43 | + } else { |
| 44 | + // OFFLINE/ONLINE event: update online status (1 removes, -1 adds) |
| 45 | + ans[e.id] += e.type_ * here |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Add ALL mentions to all users |
| 50 | + for i := range ans { |
| 51 | + ans[i] += all |
| 52 | + } |
| 53 | + return ans |
| 54 | +} |
0 commit comments