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.
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.
Example 1:
Input: creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
Output: [["alice","one"],["bob","two"]]
Explanation:
The popularity of alice is 5 + 5 = 10.
The popularity of bob is 10.
The popularity of chris is 4.
alice and bob are the most popular creators.
For bob, the video with the highest view count is "two".
For alice, the videos with the highest view count are "one" and "three". Since "one" is lexicographically smaller than "three", it is included in the answer.
Example 2:
Input: creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
Output: [["alice","b"]]
Explanation:
The videos with id "b" and "c" have the highest view count.
Since "b" is lexicographically smaller than "c", it is included in the answer.
Constraints:
n == creators.length == ids.length == views.length1 <= n <= 1051 <= creators[i].length, ids[i].length <= 5creators[i] and ids[i] consist only of lowercase English letters.0 <= views[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the most popular video creator using a brute force method, we will meticulously check each creator's videos. We will count the total views for each creator by going through every single video they've made.
Here's how the algorithm would work step-by-step:
def most_popular_video_creator_brute_force(creators, views):
creator_views = {}
# Accumulate total views for each creator.
for i in range(len(creators)):
creator = creators[i]
view_count = views[i]
if creator not in creator_views:
# If a creator is not already present, create new entry.
creator_views[creator] = 0
creator_views[creator] += view_count
most_popular_creator = None
max_views = -1
# Find the creator with the maximum views.
for creator, total_views in creator_views.items():
if total_views > max_views:
# Update most popular creator if larger number of views found
most_popular_creator = creator
max_views = total_views
elif total_views == max_views:
# Break tie using lexicographical order
if creator < most_popular_creator:
most_popular_creator = creator
return most_popular_creatorTo find the most popular video creator, we'll efficiently count the views for each creator. We'll use a way to quickly keep track of the total views for each creator without having to search through all the videos every time.
Here's how the algorithm would work step-by-step:
def most_popular_video_creator(creators, videos, views):
creator_views = {}
most_popular = ''
max_views = 0
# Aggregate total views for each creator
for i in range(len(creators)):
creator = creators[i]
view_count = views[i]
if creator in creator_views:
creator_views[creator] += view_count
else:
creator_views[creator] = view_count
best_videos = {}
# Find the best video for each creator
for i in range(len(creators)):
creator = creators[i]
video = videos[i]
view_count = views[i]
if creator not in best_videos:
best_videos[creator] = (video, view_count)
else:
previous_video, previous_views = best_videos[creator]
if view_count > previous_views:
best_videos[creator] = (video, view_count)
most_popular_creator = ""
highest_views = 0
# Find the most popular creator based on total views
for creator, total_views in creator_views.items():
if total_views > highest_views:
# Update the most popular creator if current creator has more views
most_popular_creator = creator
highest_views = total_views
elif total_views == highest_views and creator < most_popular_creator:
most_popular_creator = creator
return [most_popular_creator, best_videos[most_popular_creator][0]]| Case | How to Handle |
|---|---|
| Empty input arrays (creators, ids, views) | Return an empty list since there are no creators or videos. |
| Null input arrays (creators, ids, views) | Throw IllegalArgumentException or return an empty list after null check, depending on API contract. |
| Arrays creators, ids, and views have different lengths | Throw IllegalArgumentException as the input is invalid and there is no one-to-one mapping of video, creator and view count. |
| Input arrays with a single element each | The algorithm should correctly identify the single creator and their video as the most popular. |
| Multiple creators with the same maximum popularity | The algorithm should return a list of pairs containing all such creators and their most popular videos. |
| A creator has multiple videos with the same maximum view count | The algorithm must choose the video with the smallest ID among those with the maximum view count for that creator. |
| Large number of views causing integer overflow | Use long instead of int to store views and popularity to avoid potential overflow issues. |
| Creator names are very long strings | The hash map approach should still work correctly since string keys are supported, but consider potential memory usage. |