Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2456.html

2456. Most Popular Video Creator

  • Difficulty: Medium.
  • Related Topics: Array, Hash Table, String, Sorting, Heap (Priority Queue).
  • Similar Questions: Design Video Sharing Platform, Design a Food Rating System.

Problem

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.

  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

Solution (Java, C++, Python)

  • class Solution {
        public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
            Map<String, Integer> cnt = new HashMap<>();
            Map<String, Integer> d = new HashMap<>();
            Map<String, String> x = new HashMap<>();
            int n = ids.length;
            for (int k = 0; k < n; ++k) {
                var c = creators[k];
                var i = ids[k];
                int v = views[k];
                cnt.put(c, cnt.getOrDefault(c, 0) + v);
                if (!d.containsKey(c) || d.get(c) < v || (d.get(c) == v && x.get(c).compareTo(i) > 0)) {
                    d.put(c, v);
                    x.put(c, i);
                }
            }
            List<List<String>> ans = new ArrayList<>();
            int pre = -1;
            for (var e : cnt.entrySet()) {
                String a = e.getKey();
                int b = e.getValue();
                if (b > pre) {
                    ans.clear();
                    ans.add(Arrays.asList(a, x.get(a)));
                    pre = b;
                } else if (b == pre) {
                    ans.add(Arrays.asList(a, x.get(a)));
                }
            }
            return ans;
        }
    }
    
  • class Solution {
    public:
        vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
            unordered_map<string, long> cnt;
            unordered_map<string, int> d;
            unordered_map<string, string> x;
            int n = ids.size();
            for (int k = 0; k < n; ++k) {
                auto c = creators[k];
                auto i = ids[k];
                int v = views[k];
                cnt[c] += v;
                if (!d.count(c) || d[c] < v || (d[c] == v && x[c] > i)) {
                    d[c] = v;
                    x[c] = i;
                }
            }
            long pre = -1;
            vector<vector<string>> ans;
            for (auto& [a, b] : cnt) {
                if (b > pre) {
                    ans.clear();
                    ans.push_back({a, x[a]});
                    pre = b;
                } else if (b == pre) {
                    ans.push_back({a, x[a]});
                }
            }
            return ans;
        }
    };
    
  • class Solution:
        def mostPopularCreator(
            self, creators: List[str], ids: List[str], views: List[int]
        ) -> List[List[str]]:
            cnt = defaultdict(int)
            d = {}
            x = {}
            for c, i, v in zip(creators, ids, views):
                cnt[c] += v
                if c not in d or d[c] < v or (d[c] == v and x[c] > i):
                    d[c], x[c] = v, i
            ans = []
            pre = -1
            for a, b in cnt.items():
                if b > pre:
                    ans = [[a, x[a]]]
                    pre = b
                elif b == pre:
                    ans.append([a, x[a]])
            return ans
    
    
  • func mostPopularCreator(creators []string, ids []string, views []int) (ans [][]string) {
    	cnt := map[string]int{}
    	d := map[string]int{}
    	x := map[string]string{}
    	for k, c := range creators {
    		i, v := ids[k], views[k]
    		cnt[c] += v
    		if t, ok := d[c]; !ok || t < v || (t == v && x[c] > i) {
    			d[c] = v
    			x[c] = i
    		}
    	}
    	pre := -1
    	for a, b := range cnt {
    		if b > pre {
    			ans = [][]string{[]string{a, x[a]} }
    			pre = b
    		} else if b == pre {
    			ans = append(ans, []string{a, x[a]})
    		}
    	}
    	return ans
    }
    
  • function mostPopularCreator(
        creators: string[],
        ids: string[],
        views: number[],
    ): string[][] {
        const cnt: Map<string, number> = new Map();
        const d: Map<string, number> = new Map();
        const n = ids.length;
        for (let k = 0; k < n; ++k) {
            const [c, i, v] = [creators[k], ids[k], views[k]];
            cnt.set(c, (cnt.get(c) ?? 0) + v);
            if (
                !d.has(c) ||
                views[d.get(c)!] < v ||
                (views[d.get(c)!] === v && ids[d.get(c)!] > i)
            ) {
                d.set(c, k);
            }
        }
        const mx = Math.max(...cnt.values());
        const ans: string[][] = [];
        for (const [c, x] of cnt) {
            if (x === mx) {
                ans.push([c, ids[d.get(c)!]]);
            }
        }
        return ans;
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions