LeetCode 题解工作台
最流行的视频创作者
给你两个字符串数组 creators 和 ids ,和一个整数数组 views ,所有数组的长度都是 n 。平台上第 i 个视频者是 creator[i] ,视频分配的 id 是 ids[i] ,且播放量为 views[i] 。 视频创作者的 流行度 是该创作者的 所有 视频的播放量的 总和 。请找…
5
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
我们遍历三个数组,用哈希表 统计每个创作者的播放量总和,用哈希表 记录每个创作者播放量最大的视频的下标。 然后,我们遍历哈希表 ,找出最大的播放量 ;接着再次遍历哈希表 ,找出播放量为 的创作者,将其加入答案数组中。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你两个字符串数组 creators 和 ids ,和一个整数数组 views ,所有数组的长度都是 n 。平台上第 i 个视频者是 creator[i] ,视频分配的 id 是 ids[i] ,且播放量为 views[i] 。
视频创作者的 流行度 是该创作者的 所有 视频的播放量的 总和 。请找出流行度 最高 创作者以及该创作者播放量 最大 的视频的 id 。
- 如果存在多个创作者流行度都最高,则需要找出所有符合条件的创作者。
- 如果某个创作者存在多个播放量最高的视频,则只需要找出字典序最小的
id。
返回一个二维字符串数组 answer ,其中 answer[i] = [creatori, idi] 表示 creatori 的流行度 最高 且其最流行的视频 id 是 idi ,可以按任何顺序返回该结果。
示例 1:
输入:creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4] 输出:[["alice","one"],["bob","two"]] 解释: alice 的流行度是 5 + 5 = 10 。 bob 的流行度是 10 。 chris 的流行度是 4 。 alice 和 bob 是流行度最高的创作者。 bob 播放量最高的视频 id 为 "two" 。 alice 播放量最高的视频 id 是 "one" 和 "three" 。由于 "one" 的字典序比 "three" 更小,所以结果中返回的 id 是 "one" 。
示例 2:
输入:creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2] 输出:[["alice","b"]] 解释: id 为 "b" 和 "c" 的视频都满足播放量最高的条件。 由于 "b" 的字典序比 "c" 更小,所以结果中返回的 id 是 "b" 。
提示:
n == creators.length == ids.length == views.length1 <= n <= 1051 <= creators[i].length, ids[i].length <= 5creators[i]和ids[i]仅由小写英文字母组成0 <= views[i] <= 105
解题思路
方法一:哈希表
我们遍历三个数组,用哈希表 统计每个创作者的播放量总和,用哈希表 记录每个创作者播放量最大的视频的下标。
然后,我们遍历哈希表 ,找出最大的播放量 ;接着再次遍历哈希表 ,找出播放量为 的创作者,将其加入答案数组中。
时间复杂度 ,空间复杂度 。其中 为视频数量。
class Solution:
def mostPopularCreator(
self, creators: List[str], ids: List[str], views: List[int]
) -> List[List[str]]:
cnt = defaultdict(int)
d = defaultdict(int)
for k, (c, i, v) in enumerate(zip(creators, ids, views)):
cnt[c] += v
if c not in d or views[d[c]] < v or (views[d[c]] == v and ids[d[c]] > i):
d[c] = k
mx = max(cnt.values())
return [[c, ids[d[c]]] for c, x in cnt.items() if x == mx]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) for scanning the arrays and populating the hash table. Space complexity is O(n) for storing cumulative views and top videos per creator. Sorting is avoided by lexicographical comparisons during accumulation. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Checks if candidate uses hash tables for per-creator aggregation
- question_mark
Looks for proper handling of ties in video views using lexicographical order
- question_mark
Wants linear scanning without redundant sorting for performance
常见陷阱
外企场景- error
Not handling multiple videos with same id correctly
- error
Forgetting to compare video ids lexicographically in case of view ties
- error
Using nested loops instead of hash table accumulation causing TLE on large n
进阶变体
外企场景- arrow_right_alt
Return only the single most popular creator instead of all ties
- arrow_right_alt
Include a threshold filter for minimum views to consider videos
- arrow_right_alt
Extend to top k creators by popularity instead of just the maximum