Skip to content

Commit 5ef01d0

Browse files
committed
2025-12-12
1 parent a849db8 commit 5ef01d0

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [3433. 统计用户被提及情况](https://leetcode.cn/problems/count-mentions-per-user/description/?envType=daily-question&envId=2025-12-12)
2+
3+
> **日期**:2025-12-12
4+
> **所用时间**:5min
5+
6+
## 1. 模拟
7+
8+
给定一个聊天室的事件流(如用户发送消息、设置免打扰等),统计每个用户被"提及"的次数。事件有如下几种类型:
9+
- `MESSAGE` 消息事件,可能带有特殊提及如 `ALL`(所有人)、`HERE`(在场的用户)或明确的用户ID集合。
10+
- 其他类型,如设置免打扰,将使某些用户在一段时间内不能被 `HERE` 提及。
11+
12+
**思路**:
13+
1. 用一个数组 `mentions` 统计每个用户被有效提及的次数。
14+
2. 用户的"可被提及状态"用`status`数组记录,每当有免打扰等影响提及时更新其状态或有效时刻。
15+
3. 按事件时间排序,逐条处理。遇到`MESSAGE`事件时,根据提及类型(ALL/HERE/显式用户集)判断哪些用户需要计数。
16+
4. 最终返回每个用户被提及的总次数。
17+
18+
- 时间复杂度: $O(mn+mlogm+logU+L)$,其中 $m$ 是 $events$ 的长度,$n$ 是 $numberOfUsers$,$U \leq 10$
19+
- $U$ 是时间戳的最大值
20+
- $L$ 是所有 $mentions_string$ 的长度之和
21+
- 空间复杂度: $O(n)$,其中 $n$ 是 $numberOfUsers$
22+
23+
**Python3**
24+
25+
```python
26+
class Solution:
27+
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
28+
status = [0] * numberOfUsers
29+
mentions = [0] * numberOfUsers
30+
31+
events.sort(key=lambda x: (int(x[1]), -ord(x[0][0])))
32+
33+
for type, timestamp, mention in events:
34+
if type == 'MESSAGE':
35+
if mention == 'ALL':
36+
scope = list(range(numberOfUsers))
37+
elif mention == 'HERE':
38+
scope = [i for i in range(numberOfUsers) if status[i] <= int(timestamp)]
39+
else:
40+
users = list(mention.split())
41+
scope = [int(i[2:]) for i in users]
42+
for i in scope:
43+
mentions[i] += 1
44+
else:
45+
status[int(mention)] = int(timestamp) + 60
46+
return mentions
47+
```

leetcode/7-面试经典 150 题/5-哈希表/49. 字母异位词分组.md

Whitespace-only changes.

0 commit comments

Comments
 (0)