Leetcode 2456: Most Popular Video Creator

grid47
grid47
Exploring patterns and algorithms
Mar 6, 2024 7 min read

You are given three arrays: ‘creators’, ‘ids’, and ‘views’. Each index i corresponds to a video created by the creator ‘creators[i]’, with an id ‘ids[i]’, and a number of views ‘views[i]’. The task is to find the most popular creators based on the sum of views of all their videos and identify the id of their most viewed video. If multiple creators have the same popularity, return all of them. For each creator, if they have multiple videos with the highest view count, return the lexicographically smallest id of their most viewed video.
Problem
Approach
Steps
Complexity
Input: You are given three arrays: 'creators' (a list of strings), 'ids' (a list of strings), and 'views' (a list of integers). All arrays have the same length n, representing the number of videos.
Example: creators = ['anna', 'bob', 'anna', 'chris'], ids = ['one', 'two', 'three', 'four'], views = [5, 10, 5, 4]
Constraints:
• 1 <= n <= 10^5
• 1 <= creators[i].length, ids[i].length <= 5
• creators[i] and ids[i] consist of lowercase English letters only
• 0 <= views[i] <= 10^5
Output: Return a 2D array where each element is a pair of creator and their most popular video's id. If there are multiple creators with the highest popularity, include all of them. If a creator has multiple videos with the highest views, return the lexicographically smallest id.
Example: [['anna', 'one'], ['bob', 'two']]
Constraints:
• The answer can be in any order.
Goal: To calculate the popularity of creators based on the sum of views of their videos and identify the id of their most popular video.
Steps:
• 1. Iterate through each video and accumulate views for each creator.
• 2. Store the video details for each creator in a way that allows easy identification of the most viewed video.
• 3. After processing all the videos, identify the creator(s) with the highest total views.
• 4. For each of these creators, find their video with the highest views, and if there are ties, choose the lexicographically smallest video id.
Goal: Ensure the solution is optimized to handle large inputs within the provided constraints.
Steps:
• The input size can be up to 100,000, so the solution should run in O(n) time complexity.
Assumptions:
• The video ids are not necessarily unique across creators, but each video id is unique for each creator.
Input: creators = ['anna', 'bob', 'anna', 'chris'], ids = ['one', 'two', 'three', 'four'], views = [5, 10, 5, 4]
Explanation: In this example, 'anna' has a total of 10 views (from videos 'one' and 'three'). 'bob' has 10 views (from video 'two'). 'chris' has 4 views. 'anna' and 'bob' are the most popular creators. For 'anna', the lexicographically smaller video id with the highest views is 'one'. For 'bob', the video id with the highest views is 'two'.

Input: creators = ['anna', 'anna', 'anna'], ids = ['a', 'b', 'c'], views = [1, 2, 2]
Explanation: Here, 'anna' is the only creator, and the most viewed video is 'b' because it has 2 views, which is lexicographically smaller than 'c'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus