-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2456-most-popular-video-creator.js
More file actions
67 lines (59 loc) · 2.14 KB
/
2456-most-popular-video-creator.js
File metadata and controls
67 lines (59 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* 2456. Most Popular Video Creator
* https://leetcode.com/problems/most-popular-video-creator/
* Difficulty: Medium
*
* You are given two string arrays creators and ids, and an integer array views, all of length n.
* The ith video on a platform was created by creators[i], has an id of ids[i], and has views[i]
* views.
*
* The popularity of a creator is the sum of the number of views on all of the creator's videos.
* Find the creator with the highest popularity and the id of their most viewed video.
* - If multiple creators have the highest popularity, find all of them.
* - If multiple videos have the highest view count for a creator, find the lexicographically
* smallest id.
*
* Note: It is possible for different videos to have the same id, meaning that ids do not uniquely
* identify a video. For example, two videos with the same ID are considered as distinct videos
* with their own viewcount.
*
* Return a 2D array of strings answer where answer[i] = [creatorsi, idi] means that creatorsi
* has the highest popularity and idi is the id of their most popular video. The answer can be
* returned in any order.
*/
/**
* @param {string[]} creators
* @param {string[]} ids
* @param {number[]} views
* @return {string[][]}
*/
var mostPopularCreator = function(creators, ids, views) {
const map = new Map();
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
const videoId = ids[i];
const videoViews = views[i];
if (!map.has(creator)) {
map.set(creator, {
totalViews: 0,
mostViewedCount: -1,
mostViewedId: ''
});
}
const stats = map.get(creator);
stats.totalViews += videoViews;
if (videoViews > stats.mostViewedCount || (videoViews === stats.mostViewedCount
&& videoId < stats.mostViewedId)) {
stats.mostViewedCount = videoViews;
stats.mostViewedId = videoId;
}
}
const maxViews = Math.max(...Array.from(map.values()).map(s => s.totalViews));
const result = [];
for (const [creator, stats] of map) {
if (stats.totalViews === maxViews) {
result.push([creator, stats.mostViewedId]);
}
}
return result;
};