Skip to content

Commit 26813c7

Browse files
committed
update: 添加问题“3433.统计用户被提及情况”的代码和题解 (#1220)
3433: AC.py (#1219) + word(en) py - AC,47.06%,91.18% Signed-off-by: LetMeFly666 <[email protected]>
1 parent 1cf356c commit 26813c7

7 files changed

+198
-39
lines changed

.commitmsg

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
3531: AC.cpp+py+go+java+rust (#1217)
1+
3433: AC.py (#1219) + word(en)
22

3-
cpp(1e5version) - AC,27.12%,27.12%
4-
cpp - AC,55.93%,55.93%
5-
py - AC,60.46%,86.05%
6-
go - AC,44.44%,33.33%
7-
rust - AC,50.00%,-%
8-
9-
是不是这道题通过者还比较少(看超越百分比有感)
3+
py - AC,47.06%,91.18%
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-12-12 13:29:58
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-12-12 13:42:16
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
11+
ans: List[int] = [0] * numberOfUsers
12+
for action, time, who in events:
13+
if action == "OFFLINE":
14+
continue
15+
if who == "ALL":
16+
ans = [x + 1 for x in ans]
17+
elif who == "HERE":
18+
ans = [x + 1 for x in ans]
19+
for a, t, w in events:
20+
if a == "OFFLINE" and int(time) - 60 < int(t) <= int(time):
21+
ans[int(w)] -= 1
22+
else:
23+
for i in (int(w[2:]) for w in who.split(" ")):
24+
ans[i] += 1
25+
return ans # 差点忘了return

Codes/3531-count-covered-buildings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
class Solution {
1212
public:
1313
int countCoveredBuildings(int n, vector<vector<int>>& buildings) {
14-
vector<int> xm(100001, 100001), xM(100001), ym(100001, 100001), yM(100001);
14+
n++;
15+
vector<int> xm(n, n), xM(n), ym(n, n), yM(n);
1516
for (vector<int>& building : buildings) {
1617
int x = building[0], y = building[1];
1718
xm[x] = min(xm[x], y);

Codes/3531-count-covered-buildings_better.cpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@
10451045
|3392.统计符合条件长度为3的子数组数目|简单|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147603015" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/solutions/3665081/letmefly-3392tong-ji-fu-he-tiao-jian-cha-h935/" target="_blank">LeetCode题解</a>|
10461046
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|
10471047
|3423.循环数组中相邻元素的最大差值|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/12/LeetCode%203423.%E5%BE%AA%E7%8E%AF%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148620597" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/solutions/3699053/letmefly-3423xun-huan-shu-zu-zhong-xiang-cnde/" target="_blank">LeetCode题解</a>|
1048+
|3433.统计用户被提及情况|中等|<a href="https://leetcode.cn/problems/count-mentions-per-user/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/12/LeetCode%203433.%E7%BB%9F%E8%AE%A1%E7%94%A8%E6%88%B7%E8%A2%AB%E6%8F%90%E5%8F%8A%E6%83%85%E5%86%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155861707" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-mentions-per-user/solutions/3855685/letmefly-3433tong-ji-yong-hu-bei-ti-ji-q-6b8p/" target="_blank">LeetCode题解</a>|
10481049
|3439.重新安排会议得到最多空余时间I|中等|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/13/LeetCode%203439.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E4%BC%9A%E8%AE%AE%E5%BE%97%E5%88%B0%E6%9C%80%E5%A4%9A%E7%A9%BA%E4%BD%99%E6%97%B6%E9%97%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149317430" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/solutions/3722937/letmefly-3439zhong-xin-an-pai-hui-yi-de-r3t9e/" target="_blank">LeetCode题解</a>|
10491050
|3442.奇偶频次间的最大差值I|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148570777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/solutions/3697605/letmefly-3442qi-ou-pin-ci-jian-de-zui-da-nwwx/" target="_blank">LeetCode题解</a>|
10501051
|3459.最长V形对角线段的长度|困难|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/31/LeetCode%203459.%E6%9C%80%E9%95%BFV%E5%BD%A2%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%AE%B5%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151050078" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/solutions/3768545/letmefly-3459zui-chang-v-xing-dui-jiao-x-gu8q/" target="_blank">LeetCode题解</a>|
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
title: 3433.统计用户被提及情况:(大)模拟
3+
date: 2025-12-12 18:30:01
4+
tags: [题解, LeetCode, 中等, 数组, 数学, 排序, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3433.统计用户被提及情况:(大)模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-mentions-per-user/](https://leetcode.cn/problems/count-mentions-per-user/)
11+
12+
<p>给你一个整数&nbsp;<code>numberOfUsers</code>&nbsp;表示用户总数,另有一个大小为 <code>n x 3</code> 的数组&nbsp;<code>events</code>&nbsp;。</p>
13+
14+
<p>每个&nbsp;<code inline="">events[i]</code>&nbsp;都属于下述两种类型之一:</p>
15+
16+
<ol>
17+
<li><strong>消息事件(Message Event):</strong><code>["MESSAGE", "timestamp<sub>i</sub>", "mentions_string<sub>i</sub>"]</code>
18+
19+
<ul>
20+
<li>事件表示在&nbsp;<code>timestamp<sub>i</sub></code>&nbsp;时,一组用户被消息提及。</li>
21+
<li><code>mentions_string<sub>i</sub></code>&nbsp;字符串包含下述标识符之一:
22+
<ul>
23+
<li><code>id&lt;number&gt;</code>:其中&nbsp;<code>&lt;number&gt;</code>&nbsp;是一个区间&nbsp;<code>[0,numberOfUsers - 1]</code>&nbsp;内的整数。可以用单个空格分隔&nbsp;<strong>多个</strong> id ,并且 id 可能重复。此外,这种形式可以提及离线用户。</li>
24+
<li><code>ALL</code>:提及 <strong>所有</strong> 用户。</li>
25+
<li><code>HERE</code>:提及所有 <strong>在线</strong> 用户。</li>
26+
</ul>
27+
</li>
28+
</ul>
29+
</li>
30+
<li><strong>离线事件(Offline Event):</strong><code>["OFFLINE", "timestamp<sub>i</sub>", "id<sub>i</sub>"]</code>
31+
<ul>
32+
<li>事件表示用户&nbsp;<code>id<sub>i</sub></code>&nbsp;在&nbsp;<code>timestamp<sub>i</sub></code>&nbsp;时变为离线状态 <strong>60 个单位时间</strong>。用户会在&nbsp;<code>timestamp<sub>i</sub> + 60</code>&nbsp;时自动再次上线。</li>
33+
</ul>
34+
</li>
35+
</ol>
36+
37+
<p>返回数组&nbsp;<code>mentions</code>&nbsp;,其中&nbsp;<code>mentions[i]</code>&nbsp;表示 &nbsp;id 为 &nbsp;<code>i</code>&nbsp;的用户在所有&nbsp;<code>MESSAGE</code>&nbsp;事件中被提及的次数。</p>
38+
39+
<p>最初所有用户都处于在线状态,并且如果某个用户离线或者重新上线,其对应的状态变更将会在所有相同时间发生的消息事件之前进行处理和同步。</p>
40+
41+
<p><strong>注意 </strong>在单条消息中,同一个用户可能会被提及多次。每次提及都需要被 <strong>分别</strong> 统计。</p>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><b>示例 1:</b></p>
46+
47+
<div class="example-block">
48+
<p><span class="example-io"><b>输入:</b>numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]</span></p>
49+
50+
<p><span class="example-io"><b>输出:</b>[2,2]</span></p>
51+
52+
<p><b>解释:</b></p>
53+
54+
<p>最初,所有用户都在线。</p>
55+
56+
<p>时间戳 10 ,<code>id1</code>&nbsp;&nbsp;<code>id0</code>&nbsp;被提及,<code>mentions = [1,1]</code></p>
57+
58+
<p>时间戳 11 ,<code>id0</code>&nbsp;<strong>离线</strong> 。</p>
59+
60+
<p>时间戳 71 ,<code>id0</code>&nbsp;再次 <strong>上线</strong>&nbsp;并且&nbsp;<code>"HERE"</code>&nbsp;被提及,<code>mentions = [2,2]</code></p>
61+
</div>
62+
63+
<p><b>示例 2:</b></p>
64+
65+
<div class="example-block">
66+
<p><span class="example-io"><b>输入:</b>numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]</span></p>
67+
68+
<p><span class="example-io"><b>输出:</b>[2,2]</span></p>
69+
70+
<p><b>解释:</b></p>
71+
72+
<p>最初,所有用户都在线。</p>
73+
74+
<p>时间戳 10 ,<code>id1</code>&nbsp;&nbsp;<code>id0</code>&nbsp;被提及,<code>mentions = [1,1]</code></p>
75+
76+
<p>时间戳 11 ,<code>id0</code>&nbsp;<strong>离线</strong> 。</p>
77+
78+
<p>时间戳 12 ,<code>"ALL"</code>&nbsp;被提及。这种方式将会包括所有离线用户,所以&nbsp;<code>id0</code>&nbsp;&nbsp;<code>id1</code>&nbsp;都被提及,<code>mentions = [2,2]</code></p>
79+
</div>
80+
81+
<p><b>示例 3:</b></p>
82+
83+
<div class="example-block">
84+
<p><span class="example-io"><b>输入:</b>numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]]</span></p>
85+
86+
<p><span class="example-io"><b>输出:</b>[0,1]</span></p>
87+
88+
<p><b>解释:</b></p>
89+
90+
<p>最初,所有用户都在线。</p>
91+
92+
<p>时间戳 10 ,<code>id0</code>&nbsp;<strong>离线</strong>&nbsp;<b>。</b></p>
93+
94+
<p>时间戳 12 ,<code>"HERE"</code>&nbsp;被提及。由于&nbsp;<code>id0</code>&nbsp;仍处于离线状态,其将不会被提及,<code>mentions = [0,1]</code></p>
95+
</div>
96+
97+
<p>&nbsp;</p>
98+
99+
<p><b>提示:</b></p>
100+
101+
<ul>
102+
<li><code>1 &lt;= numberOfUsers &lt;= 100</code></li>
103+
<li><code>1 &lt;= events.length &lt;= 100</code></li>
104+
<li><code>events[i].length == 3</code></li>
105+
<li><code>events[i][0]</code>&nbsp;的值为&nbsp;<code>MESSAGE</code>&nbsp;或&nbsp;<code>OFFLINE</code>&nbsp;。</li>
106+
<li><code>1 &lt;= int(events[i][1]) &lt;= 10<sup>5</sup></code></li>
107+
<li>在任意 <code>"MESSAGE"</code>&nbsp;事件中,以&nbsp;<code>id&lt;number&gt;</code>&nbsp;形式提及的用户数目介于&nbsp;<code>1</code>&nbsp;和&nbsp;<code>100</code>&nbsp;之间。</li>
108+
<li><code>0 &lt;= &lt;number&gt; &lt;= numberOfUsers - 1</code></li>
109+
<li>题目保证 <code>OFFLINE</code>&nbsp;引用的用户 id 在事件发生时处于 <strong>在线</strong> 状态。</li>
110+
</ul>
111+
112+
113+
114+
## 解题方法:模拟
115+
116+
最多100个人,最多100个事件,所以直接暴力模拟就好了。
117+
118+
创建一个答案数组,初始值全部为0,接着开始遍历事件:
119+
120+
* 如果事件是`OFFLINE`,则什么都不做,直接continue。否则一定是消息事件:
121+
* 如果事件是`ALL`,则每人+1
122+
* 如果事件是`HERE`,则先每人+1,然后再遍历一遍事件数组,如果存在60时间内的下线时间,则此人-1
123+
* 否则(@指定人),被提及到的人们+1
124+
125+
以上。
126+
127+
### 时空复杂度分析
128+
129+
令$e=len(events)$,$n=numberOfUsers$:
130+
131+
+ 单次操作时间复杂度:下线$O(1)$、所有人$O(n)$、在线人$O(n+e)$、指定人$O(len(events[i][2]))$
132+
+ 总空间复杂度$O(n)$
133+
134+
### AC代码
135+
136+
#### Python
137+
138+
```python
139+
'''
140+
LastEditTime: 2025-12-12 13:42:16
141+
'''
142+
from typing import List
143+
144+
class Solution:
145+
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
146+
ans: List[int] = [0] * numberOfUsers
147+
for action, time, who in events:
148+
if action == "OFFLINE":
149+
continue
150+
if who == "ALL":
151+
ans = [x + 1 for x in ans]
152+
elif who == "HERE":
153+
ans = [x + 1 for x in ans]
154+
for a, t, w in events:
155+
if a == "OFFLINE" and int(time) - 60 < int(t) <= int(time):
156+
ans[int(w)] -= 1
157+
else:
158+
for i in (int(w[2:]) for w in who.split(" ")):
159+
ans[i] += 1
160+
return ans # 差点忘了return
161+
```
162+
163+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/155861707)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/12/LeetCode%203433.%E7%BB%9F%E8%AE%A1%E7%94%A8%E6%88%B7%E8%A2%AB%E6%8F%90%E5%8F%8A%E6%83%85%E5%86%B5/)~
164+
>
165+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,9 @@ categories: [自用]
16611661
|||
16621662
|concerted|adj. 努力的,共同策划决定的,同心协力的|
16631663
|proletarian|adj. 无产阶级的,工人阶级的,无产者的<br/>n. 无产者,无产阶级|
1664+
|||
1665+
|pebble|n. 鹅卵石<br/>v. 用石子扔,用卵石覆盖|
1666+
|dome|n. 穹顶,圆屋顶,穹顶建筑物|
16641667

16651668
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
16661669
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)