3433. Count Mentions Per User #2527
-
|
Topics: You are given an integer Each
Return an array All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp. Note that a user can be mentioned multiple times in a single message event, and each mention should be counted separately. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to track user mentions from MESSAGE events, considering that users can go offline and come back online after 60 time units. We must handle three types of mentions:
Approach:
Let's implement this solution in PHP: 3433. Count Mentions Per User <?php
/**
* @param Integer $numberOfUsers
* @param String[][] $events
* @return Integer[]
*/
function countMentions($numberOfUsers, $events) {
// Sort events by timestamp
usort($events, function($a, $b) {
$timeA = (int)$a[1];
$timeB = (int)$b[1];
if ($timeA === $timeB) {
// If same timestamp, ensure OFFLINE events come before MESSAGE events
$order = ['OFFLINE' => 0, 'MESSAGE' => 1];
return $order[$a[0]] <=> $order[$b[0]];
}
return $timeA <=> $timeB;
});
// Initialize mentions count for each user
$mentions = array_fill(0, $numberOfUsers, 0);
// Track when each user comes back online (timestamp)
$offlineUntil = array_fill(0, $numberOfUsers, 0);
// Process events in order
foreach ($events as $event) {
$type = $event[0];
$timestamp = (int)$event[1];
// First, check if any users should come back online at this timestamp
for ($i = 0; $i < $numberOfUsers; $i++) {
if ($offlineUntil[$i] > 0 && $timestamp >= $offlineUntil[$i]) {
$offlineUntil[$i] = 0; // User is back online
}
}
if ($type === 'OFFLINE') {
$userId = (int)$event[2];
// User goes offline for 60 time units
$offlineUntil[$userId] = $timestamp + 60;
}
elseif ($type === 'MESSAGE') {
$mentionsStr = $event[2];
if ($mentionsStr === 'ALL') {
// Mention all users
for ($i = 0; $i < $numberOfUsers; $i++) {
$mentions[$i]++;
}
}
elseif ($mentionsStr === 'HERE') {
// Mention only online users
for ($i = 0; $i < $numberOfUsers; $i++) {
if ($offlineUntil[$i] === 0) { // User is online
$mentions[$i]++;
}
}
}
else {
// Parse individual user mentions
$tokens = explode(' ', $mentionsStr);
foreach ($tokens as $token) {
if (strpos($token, 'id') === 0) {
$userId = (int)substr($token, 2);
if ($userId >= 0 && $userId < $numberOfUsers) {
$mentions[$userId]++;
}
}
}
}
}
}
return $mentions;
}
// Test cases
echo countMentions(2, [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]) . "\n"; // Output: [2,2]
echo countMentions(2, [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]) . "\n"; // Output: [2,2]
echo countMentions(2, [["OFFLINE","10","0"],["MESSAGE","12","HERE"]]) . "\n"; // Output: [0,1]
?>Explanation:
Complexity Analysis
|
Beta Was this translation helpful? Give feedback.
We need to track user mentions from MESSAGE events, considering that users can go offline and come back online after 60 time units. We must handle three types of mentions:
id<number>: Mention specific users (can be multiple, duplicate)ALL: Mention all users (online and offline)HERE: Mention only online usersApproach:
Let's implement this solutio…