Problem: 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
`creator[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.

Return a 2D array of strings `answer` where `answer[i] = [creatori, idi]`
means that `creatori` has the highest popularity and `idi` is the id of their
most popular video. The answer can be returned in any order.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Creator:
  def __init__(self, popularity: int, videoId: str, maxView: int):
    self.popularity = popularity
    self.videoId = videoId
    self.maxView = maxView


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


Java solution:
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;
  }
}


C++ solution:
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;
  }
};