LeetCode 2456: Most Popular Video Creator Solution

Master LeetCode problem 2456 (Most Popular Video Creator), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2456. Most Popular Video Creator

Problem Explanation

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)

Solution Code

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;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2456 (Most Popular Video Creator)?

This page provides optimized solutions for LeetCode problem 2456 (Most Popular Video Creator) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2456 (Most Popular Video Creator)?

The time complexity for LeetCode 2456 (Most Popular Video Creator) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2456 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2456 in Java, C++, or Python.

Back to LeetCode Solutions