|
| 1 | +/** |
| 2 | + * 3433. Count Mentions Per User |
| 3 | + * https://leetcode.com/problems/count-mentions-per-user/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are given an integer numberOfUsers representing the total number of users and an |
| 7 | + * array events of size n x 3. |
| 8 | + * |
| 9 | + * Each events[i] can be either of the following two types: |
| 10 | + * 1. Message Event: ["MESSAGE", "timestampi", "mentions_stringi"] |
| 11 | + * - This event indicates that a set of users was mentioned in a message at timestampi. |
| 12 | + * - The mentions_stringi string can contain one of the following tokens: |
| 13 | + * - id<number>: where <number> is an integer in range [0,numberOfUsers - 1]. There can |
| 14 | + * be multiple ids separated by a single whitespace and may contain duplicates. This |
| 15 | + * can mention even the offline users. |
| 16 | + * - ALL: mentions all users. |
| 17 | + * - HERE: mentions all online users. |
| 18 | + * 2. Offline Event: ["OFFLINE", "timestampi", "idi"] |
| 19 | + * - This event indicates that the user idi had become offline at timestampi for 60 time units. |
| 20 | + * The user will automatically be online again at time timestampi + 60. |
| 21 | + * |
| 22 | + * Return an array mentions where mentions[i] represents the number of mentions the user with |
| 23 | + * id i has across all MESSAGE events. |
| 24 | + * |
| 25 | + * All users are initially online, and if a user goes offline or comes back online, their status |
| 26 | + * change is processed before handling any message event that occurs at the same timestamp. |
| 27 | + * |
| 28 | + * Note that a user can be mentioned multiple times in a single message event, and each mention |
| 29 | + * should be counted separately. |
| 30 | + */ |
| 31 | + |
| 32 | +/** |
| 33 | + * @param {number} numberOfUsers |
| 34 | + * @param {string[][]} events |
| 35 | + * @return {number[]} |
| 36 | + */ |
| 37 | +var countMentions = function(numberOfUsers, events) { |
| 38 | + const result = new Array(numberOfUsers).fill(0); |
| 39 | + const onlineAt = new Array(numberOfUsers).fill(0); |
| 40 | + |
| 41 | + events.sort((a, b) => { |
| 42 | + const timeCompare = parseInt(a[1], 10) - parseInt(b[1], 10); |
| 43 | + if (timeCompare !== 0) return timeCompare; |
| 44 | + return a[0] === 'MESSAGE' ? 1 : -1; |
| 45 | + }); |
| 46 | + |
| 47 | + for (const [type, timestampStr, data] of events) { |
| 48 | + const timestamp = parseInt(timestampStr, 10); |
| 49 | + |
| 50 | + if (type === 'OFFLINE') { |
| 51 | + const userId = parseInt(data, 10); |
| 52 | + onlineAt[userId] = timestamp + 60; |
| 53 | + } else { |
| 54 | + if (data === 'ALL') { |
| 55 | + for (let i = 0; i < numberOfUsers; i++) { |
| 56 | + result[i]++; |
| 57 | + } |
| 58 | + } else if (data === 'HERE') { |
| 59 | + for (let i = 0; i < numberOfUsers; i++) { |
| 60 | + if (onlineAt[i] <= timestamp) { |
| 61 | + result[i]++; |
| 62 | + } |
| 63 | + } |
| 64 | + } else { |
| 65 | + const ids = data.replace(/id/g, '').split(' '); |
| 66 | + for (const id of ids) { |
| 67 | + result[parseInt(id, 10)]++; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return result; |
| 74 | +}; |
0 commit comments