|
| 1 | +<h2><a href="https://leetcode.com/problems/count-mentions-per-user">3721. Count Mentions Per User</a></h2><h3>Medium</h3><hr><p>You are given an integer <code>numberOfUsers</code> representing the total number of users and an array <code>events</code> of size <code>n x 3</code>.</p> |
| 2 | + |
| 3 | +<p>Each <code inline="">events[i]</code> can be either of the following two types:</p> |
| 4 | + |
| 5 | +<ol> |
| 6 | + <li><strong>Message Event:</strong> <code>["MESSAGE", "timestamp<sub>i</sub>", "mentions_string<sub>i</sub>"]</code> |
| 7 | + |
| 8 | + <ul> |
| 9 | + <li>This event indicates that a set of users was mentioned in a message at <code>timestamp<sub>i</sub></code>.</li> |
| 10 | + <li>The <code>mentions_string<sub>i</sub></code> string can contain one of the following tokens: |
| 11 | + <ul> |
| 12 | + <li><code>id<number></code>: where <code><number></code> is an integer in range <code>[0,numberOfUsers - 1]</code>. There can be <strong>multiple</strong> ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.</li> |
| 13 | + <li><code>ALL</code>: mentions <strong>all</strong> users.</li> |
| 14 | + <li><code>HERE</code>: mentions all <strong>online</strong> users.</li> |
| 15 | + </ul> |
| 16 | + </li> |
| 17 | + </ul> |
| 18 | + </li> |
| 19 | + <li><strong>Offline Event:</strong> <code>["OFFLINE", "timestamp<sub>i</sub>", "id<sub>i</sub>"]</code> |
| 20 | + <ul> |
| 21 | + <li>This event indicates that the user <code>id<sub>i</sub></code> had become offline at <code>timestamp<sub>i</sub></code> for <strong>60 time units</strong>. The user will automatically be online again at time <code>timestamp<sub>i</sub> + 60</code>.</li> |
| 22 | + </ul> |
| 23 | + </li> |
| 24 | +</ol> |
| 25 | + |
| 26 | +<p>Return an array <code>mentions</code> where <code>mentions[i]</code> represents the number of mentions the user with id <code>i</code> has across all <code>MESSAGE</code> events.</p> |
| 27 | + |
| 28 | +<p>All users are initially online, and if a user goes offline or comes back online, their status change is processed <em>before</em> handling any message event that occurs at the same timestamp.</p> |
| 29 | + |
| 30 | +<p><strong>Note </strong>that a user can be mentioned <strong>multiple</strong> times in a <strong>single</strong> message event, and each mention should be counted <strong>separately</strong>.</p> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong class="example">Example 1:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block"> |
| 36 | +<p><strong>Input:</strong> <span class="example-io">numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]</span></p> |
| 37 | + |
| 38 | +<p><strong>Output:</strong> <span class="example-io">[2,2]</span></p> |
| 39 | + |
| 40 | +<p><strong>Explanation:</strong></p> |
| 41 | + |
| 42 | +<p>Initially, all users are online.</p> |
| 43 | + |
| 44 | +<p>At timestamp 10, <code>id1</code> and <code>id0</code> are mentioned. <code>mentions = [1,1]</code></p> |
| 45 | + |
| 46 | +<p>At timestamp 11, <code>id0</code> goes <strong>offline.</strong></p> |
| 47 | + |
| 48 | +<p>At timestamp 71, <code>id0</code> comes back <strong>online</strong> and <code>"HERE"</code> is mentioned. <code>mentions = [2,2]</code></p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p><strong class="example">Example 2:</strong></p> |
| 52 | + |
| 53 | +<div class="example-block"> |
| 54 | +<p><strong>Input:</strong> <span class="example-io">numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]</span></p> |
| 55 | + |
| 56 | +<p><strong>Output:</strong> <span class="example-io">[2,2]</span></p> |
| 57 | + |
| 58 | +<p><strong>Explanation:</strong></p> |
| 59 | + |
| 60 | +<p>Initially, all users are online.</p> |
| 61 | + |
| 62 | +<p>At timestamp 10, <code>id1</code> and <code>id0</code> are mentioned. <code>mentions = [1,1]</code></p> |
| 63 | + |
| 64 | +<p>At timestamp 11, <code>id0</code> goes <strong>offline.</strong></p> |
| 65 | + |
| 66 | +<p>At timestamp 12, <code>"ALL"</code> is mentioned. This includes offline users, so both <code>id0</code> and <code>id1</code> are mentioned. <code>mentions = [2,2]</code></p> |
| 67 | +</div> |
| 68 | + |
| 69 | +<p><strong class="example">Example 3:</strong></p> |
| 70 | + |
| 71 | +<div class="example-block"> |
| 72 | +<p><strong>Input:</strong> <span class="example-io">numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]]</span></p> |
| 73 | + |
| 74 | +<p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> |
| 75 | + |
| 76 | +<p><strong>Explanation:</strong></p> |
| 77 | + |
| 78 | +<p>Initially, all users are online.</p> |
| 79 | + |
| 80 | +<p>At timestamp 10, <code>id0</code> goes <strong>offline.</strong></p> |
| 81 | + |
| 82 | +<p>At timestamp 12, <code>"HERE"</code> is mentioned. Because <code>id0</code> is still offline, they will not be mentioned. <code>mentions = [0,1]</code></p> |
| 83 | +</div> |
| 84 | + |
| 85 | +<p> </p> |
| 86 | +<p><strong>Constraints:</strong></p> |
| 87 | + |
| 88 | +<ul> |
| 89 | + <li><code>1 <= numberOfUsers <= 100</code></li> |
| 90 | + <li><code>1 <= events.length <= 100</code></li> |
| 91 | + <li><code>events[i].length == 3</code></li> |
| 92 | + <li><code>events[i][0]</code> will be one of <code>MESSAGE</code> or <code>OFFLINE</code>.</li> |
| 93 | + <li><code>1 <= int(events[i][1]) <= 10<sup>5</sup></code></li> |
| 94 | + <li>The number of <code>id<number></code> mentions in any <code>"MESSAGE"</code> event is between <code>1</code> and <code>100</code>.</li> |
| 95 | + <li><code>0 <= <number> <= numberOfUsers - 1</code></li> |
| 96 | + <li>It is <strong>guaranteed</strong> that the user id referenced in the <code>OFFLINE</code> event is <strong>online</strong> at the time the event occurs.</li> |
| 97 | +</ul> |
0 commit comments