Leetcode 3433. Count Mentions Per User
- Leetcode 3433. Count Mentions Per User
- 1. 解题思路
- 2. 代码实现
- 题目链接:3433. Count Mentions Per User
1. 解题思路
这一题我们首先需要将所有的event按照时间顺序进行排序,然后就只需要记录一下每一个用户下一次在线的时间以及其总的被at的次数即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
events = sorted(events, key=lambda x: (int(x[1]), 0 if x[0] == "OFFLINE" else 1))
status = [0 for _ in range(numberOfUsers)]
cnt = [0 for _ in range(numberOfUsers)]
for event in events:
t = int(event[1])
if event[0] == "MESSAGE":
mention = event[2]
if mention == "ALL":
cnt = [x+1 for x in cnt]
elif mention == "HERE":
cnt = [x+1 if t >= st else x for x, st in zip(cnt, status)]
else:
for uid in mention.split(" "):
cnt[int(uid[2:])] += 1
else:
uid = int(event[2])
status[uid] = t + 60
return cnt
提交代码评测得到:耗时38ms,占用内存18MB。