Skip to content

Commit 09354ef

Browse files
committed
Add solution #3433
1 parent 2701caf commit 09354ef

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,7 @@
27502750
3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy|
27512751
3431|[Minimum Unlocked Indices to Sort Nums](./solutions/3431-minimum-unlocked-indices-to-sort-nums.js)|Medium|
27522752
3432|[Count Partitions with Even Sum Difference](./solutions/3432-count-partitions-with-even-sum-difference.js)|Easy|
2753+
3433|[Count Mentions Per User](./solutions/3433-count-mentions-per-user.js)|Medium|
27532754
3437|[Permutations III](./solutions/3437-permutations-iii.js)|Medium|
27542755
3439|[Reschedule Meetings for Maximum Free Time I](./solutions/3439-reschedule-meetings-for-maximum-free-time-i.js)|Medium|
27552756
3440|[Reschedule Meetings for Maximum Free Time II](./solutions/3440-reschedule-meetings-for-maximum-free-time-ii.js)|Medium|
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)