Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

2456. Most Popular Video Creator

Explanation

To solve this problem, we need to calculate the popularity of each creator by summing up the views of all their videos. Then we find the creators with the highest popularity and for each of these creators, we determine their most viewed video based on the given constraints.

  1. Create a hashmap to store the total views for each creator.
  2. Create a hashmap to store the most viewed video id for each creator.
  3. Iterate through the input arrays and populate the above hashmaps.
  4. Find the maximum popularity among all creators.
  5. Iterate through the creators to find the ones with maximum popularity.
  6. For each creator with maximum popularity, find their most viewed video based on the constraints.
  7. Return the result in the required format.

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

import java.util.*;

class Solution {
    public List<List<String>> mostPopularVideoCreator(String[] creators, String[] ids, int[] views) {
        Map<String, Integer> popularity = new HashMap<>();
        Map<String, String> mostViewed = new HashMap<>();

        for (int i = 0; i < creators.length; i++) {
            popularity.put(creators[i], popularity.getOrDefault(creators[i], 0) + views[i]);
            if (!mostViewed.containsKey(creators[i]) || views[i] > popularity.get(mostViewed.get(creators[i]))) {
                mostViewed.put(creators[i], ids[i]);
            } else if (views[i] == popularity.get(mostViewed.get(creators[i])) && ids[i].compareTo(mostViewed.get(creators[i])) < 0) {
                mostViewed.put(creators[i], ids[i]);
            }
        }

        int maxPopularity = Collections.max(popularity.values());
        List<List<String>> result = new ArrayList<>();
        
        for (String creator : popularity.keySet()) {
            if (popularity.get(creator) == maxPopularity) {
                result.add(Arrays.asList(creator, mostViewed.get(creator)));
            }
        }
        
        return result;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.