2456. Most Popular Video Creator LeetCode Solution

In this guide, you will get 2456. Most Popular Video Creator LeetCode Solution with the best time and space complexity. The solution to Most Popular Video Creator problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Most Popular Video Creator solution in C++
  4. Most Popular Video Creator solution in Java
  5. Most Popular Video Creator solution in Python
  6. Additional Resources
2456. Most Popular Video Creator LeetCode Solution image

Problem Statement of Most Popular Video Creator

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.

See also  1326. Minimum Number of Taps to Open to Water a Garden LeetCode Solution

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.length
1 <= n <= 105
1 <= creators[i].length, ids[i].length <= 5
creators[i] and ids[i] consist only of lowercase English letters.
0 <= views[i] <= 105

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2456. Most Popular Video Creator LeetCode Solution in C++

struct Creator {
  long popularity;  // the popularity sum
  string videoId;   // the video id that has the maximum view
  int maxView;      // the maximum view of the creator
};

class Solution {
 public:
  vector<vector<string>> mostPopularCreator(vector<string>& creators,
                                            vector<string>& ids,
                                            vector<int>& views) {
    vector<vector<string>> ans;
    unordered_map<string, Creator> nameToCreator;
    long maxPopularity = 0;

    for (int i = 0; i < creators.size(); ++i) {
      if (!nameToCreator.contains(creators[i])) {
        nameToCreator[creators[i]] = Creator{
            .popularity = views[i],
            .videoId = ids[i],
            .maxView = views[i],
        };
        maxPopularity = max(maxPopularity, static_cast<long>(views[i]));
        continue;
      }
      Creator& creator = nameToCreator[creators[i]];
      creator.popularity += views[i];
      maxPopularity = max(maxPopularity, static_cast<long>(creator.popularity));
      if (creator.maxView < views[i] ||
          creator.maxView == views[i] && creator.videoId > ids[i]) {
        creator.videoId = ids[i];
        creator.maxView = views[i];
      }
    }

    for (const auto& [name, creator] : nameToCreator)
      if (creator.popularity == maxPopularity)
        ans.push_back({name, creator.videoId});

    return ans;
  }
};
/* code provided by PROGIEZ */

2456. Most Popular Video Creator LeetCode Solution in Java

class Creator {
  public long popularity; // the popularity sum
  public String videoId;  // the video id that has the maximum view
  public int maxView;     // the maximum view of the creator
  public Creator(long popularity, String videoId, int maxView) {
    this.popularity = popularity;
    this.videoId = videoId;
    this.maxView = maxView;
  }
}

class Solution {
  public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
    List<List<String>> ans = new ArrayList<>();
    Map<String, Creator> nameToCreator = new HashMap<>();
    long maxPopularity = 0;

    for (int i = 0; i < creators.length; ++i) {
      if (!nameToCreator.containsKey(creators[i])) {
        nameToCreator.put(creators[i], new Creator(views[i], ids[i], views[i]));
        maxPopularity = Math.max(maxPopularity, (long) views[i]);
        continue;
      }
      Creator creator = nameToCreator.get(creators[i]);
      creator.popularity += views[i];
      maxPopularity = Math.max(maxPopularity, (long) creator.popularity);
      if (creator.maxView < views[i] ||
          creator.maxView == views[i] && creator.videoId.compareTo(ids[i]) > 0) {
        creator.videoId = ids[i];
        creator.maxView = views[i];
      }
    }

    for (Map.Entry<String, Creator> entry : nameToCreator.entrySet())
      if (entry.getValue().popularity == maxPopularity)
        ans.add(Arrays.asList(entry.getKey(), entry.getValue().videoId));

    return ans;
  }
}
// code provided by PROGIEZ

2456. Most Popular Video Creator LeetCode Solution in Python

class Creator:
  def __init__(self, popularity: int, videoId: str, maxView: int):
    self.popularity = popularity  # the popularity sum
    self.videoId = videoId        # the video id that has the maximum view
    self.maxView = maxView        # the maximum view of the creator


class Solution:
  def mostPopularCreator(self, creators: list[str],
                         ids: list[str],
                         views: list[int]) -> list[list[str]]:
    ans = []
    maxPopularity = 0
    nameToCreator = {}

    for name, id, view in zip(creators, ids, views):
      if name not in nameToCreator:
        nameToCreator[name] = Creator(view, id, view)
        maxPopularity = max(maxPopularity, view)
        continue
      creator = nameToCreator[name]
      creator.popularity += view
      maxPopularity = max(maxPopularity, creator.popularity)
      if (creator.maxView < view or
              creator.maxView == view and creator.videoId > id):
        creator.videoId = id
        creator.maxView = view

    for name, creator in nameToCreator.items():
      if creator.popularity == maxPopularity:
        ans.append([name, creator.videoId])

    return ans
# code by PROGIEZ

Additional Resources

See also  1721. Swapping Nodes in a Linked List LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.