Skip to content

Commit b342f15

Browse files
committed
feat: add solutions to lc problem: No.2456
No.2456.Most Popular Video Creator
1 parent e3c7009 commit b342f15

File tree

7 files changed

+215
-184
lines changed

7 files changed

+215
-184
lines changed

solution/2400-2499/2456.Most Popular Video Creator/README.md

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ id 为 "b" 和 "c" 的视频都满足播放量最高的条件。
6161

6262
**方法一:哈希表**
6363

64-
我们用哈希表 `cnt` 统计每个创作者的视频播放量总和,用哈希表 `d``x` 记录每个创作者的最大播放量和对应的视频 `id`
64+
我们遍历三个数组,用哈希表 $cnt$ 统计每个创作者的播放量总和,用哈希表 $d$ 记录每个创作者播放量最大的视频的下标
6565

66-
然后遍历哈希表 `cnt`,找到最大视频播放量总和的创作者,将其对应的视频 `id` 加入答案数组 `ans`
66+
然后,我们遍历哈希表 $cnt$,找出最大的播放量 $mx$;接着再次遍历哈希表 $cnt$,找出播放量为 $mx$ 的创作者,将其加入答案数组中
6767

6868
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为视频数量。
6969

@@ -77,21 +77,13 @@ id 为 "b" 和 "c" 的视频都满足播放量最高的条件。
7777
class Solution:
7878
def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
7979
cnt = defaultdict(int)
80-
d = {}
81-
x = {}
82-
for c, i, v in zip(creators, ids, views):
80+
d = defaultdict(int)
81+
for k, (c, i, v) in enumerate(zip(creators, ids, views)):
8382
cnt[c] += v
84-
if c not in d or d[c] < v or (d[c] == v and x[c] > i):
85-
d[c], x[c] = v, i
86-
ans = []
87-
pre = -1
88-
for a, b in cnt.items():
89-
if b > pre:
90-
ans = [[a, x[a]]]
91-
pre = b
92-
elif b == pre:
93-
ans.append([a, x[a]])
94-
return ans
83+
if c not in d or views[d[c]] < v or (views[d[c]] == v and ids[d[c]] > i):
84+
d[c] = k
85+
mx = max(cnt.values())
86+
return [[c, ids[d[c]]] for c, x in cnt.items() if x == mx]
9587
```
9688

9789
### **Java**
@@ -101,31 +93,26 @@ class Solution:
10193
```java
10294
class Solution {
10395
public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
104-
Map<String, Integer> cnt = new HashMap<>();
105-
Map<String, Integer> d = new HashMap<>();
106-
Map<String, String> x = new HashMap<>();
10796
int n = ids.length;
97+
Map<String, Long> cnt = new HashMap<>(n);
98+
Map<String, Integer> d = new HashMap<>(n);
10899
for (int k = 0; k < n; ++k) {
109-
var c = creators[k];
110-
var i = ids[k];
111-
int v = views[k];
112-
cnt.put(c, cnt.getOrDefault(c, 0) + v);
113-
if (!d.containsKey(c) || d.get(c) < v || (d.get(c) == v && x.get(c).compareTo(i) > 0)) {
114-
d.put(c, v);
115-
x.put(c, i);
100+
String c = creators[k], i = ids[k];
101+
long v = views[k];
102+
cnt.merge(c, v, Long::sum);
103+
if (!d.containsKey(c) || views[d.get(c)] < v || (views[d.get(c)] == v && ids[d.get(c)].compareTo(i) > 0)) {
104+
d.put(c, k);
116105
}
117106
}
107+
long mx = 0;
108+
for (long x : cnt.values()) {
109+
mx = Math.max(mx, x);
110+
}
118111
List<List<String>> ans = new ArrayList<>();
119-
int pre = -1;
120112
for (var e : cnt.entrySet()) {
121-
String a = e.getKey();
122-
int b = e.getValue();
123-
if (b > pre) {
124-
ans.clear();
125-
ans.add(Arrays.asList(a, x.get(a)));
126-
pre = b;
127-
} else if (b == pre) {
128-
ans.add(Arrays.asList(a, x.get(a)));
113+
if (e.getValue() == mx) {
114+
String c = e.getKey();
115+
ans.add(List.of(c, ids[d.get(c)]));
129116
}
130117
}
131118
return ans;
@@ -139,29 +126,25 @@ class Solution {
139126
class Solution {
140127
public:
141128
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
142-
unordered_map<string, long> cnt;
129+
unordered_map<string, long long> cnt;
143130
unordered_map<string, int> d;
144-
unordered_map<string, string> x;
145131
int n = ids.size();
146132
for (int k = 0; k < n; ++k) {
147-
auto c = creators[k];
148-
auto i = ids[k];
133+
auto c = creators[k], id = ids[k];
149134
int v = views[k];
150135
cnt[c] += v;
151-
if (!d.count(c) || d[c] < v || (d[c] == v && x[c] > i)) {
152-
d[c] = v;
153-
x[c] = i;
136+
if (!d.count(c) || views[d[c]] < v || (views[d[c]] == v && ids[d[c]] > id)) {
137+
d[c] = k;
154138
}
155139
}
156-
long pre = -1;
140+
long long mx = 0;
141+
for (auto& [_, x] : cnt) {
142+
mx = max(mx, x);
143+
}
157144
vector<vector<string>> ans;
158-
for (auto& [a, b] : cnt) {
159-
if (b > pre) {
160-
ans.clear();
161-
ans.push_back({a, x[a]});
162-
pre = b;
163-
} else if (b == pre) {
164-
ans.push_back({a, x[a]});
145+
for (auto& [c, x] : cnt) {
146+
if (x == mx) {
147+
ans.push_back({c, ids[d[c]]});
165148
}
166149
}
167150
return ans;
@@ -175,32 +158,59 @@ public:
175158
func mostPopularCreator(creators []string, ids []string, views []int) (ans [][]string) {
176159
cnt := map[string]int{}
177160
d := map[string]int{}
178-
x := map[string]string{}
179161
for k, c := range creators {
180162
i, v := ids[k], views[k]
181163
cnt[c] += v
182-
if t, ok := d[c]; !ok || t < v || (t == v && x[c] > i) {
183-
d[c] = v
184-
x[c] = i
164+
if j, ok := d[c]; !ok || views[j] < v || (views[j] == v && ids[j] > i) {
165+
d[c] = k
185166
}
186167
}
187-
pre := -1
188-
for a, b := range cnt {
189-
if b > pre {
190-
ans = [][]string{[]string{a, x[a]}}
191-
pre = b
192-
} else if b == pre {
193-
ans = append(ans, []string{a, x[a]})
168+
mx := 0
169+
for _, x := range cnt {
170+
if mx < x {
171+
mx = x
194172
}
195173
}
196-
return ans
174+
for c, x := range cnt {
175+
if x == mx {
176+
ans = append(ans, []string{c, ids[d[c]]})
177+
}
178+
}
179+
return
197180
}
198181
```
199182

200183
### **TypeScript**
201184

202185
```ts
203-
186+
function mostPopularCreator(
187+
creators: string[],
188+
ids: string[],
189+
views: number[],
190+
): string[][] {
191+
const cnt: Map<string, number> = new Map();
192+
const d: Map<string, number> = new Map();
193+
const n = ids.length;
194+
for (let k = 0; k < n; ++k) {
195+
const [c, i, v] = [creators[k], ids[k], views[k]];
196+
cnt.set(c, (cnt.get(c) ?? 0) + v);
197+
if (
198+
!d.has(c) ||
199+
views[d.get(c)!] < v ||
200+
(views[d.get(c)!] === v && ids[d.get(c)!] > i)
201+
) {
202+
d.set(c, k);
203+
}
204+
}
205+
const mx = Math.max(...cnt.values());
206+
const ans: string[][] = [];
207+
for (const [c, x] of cnt) {
208+
if (x === mx) {
209+
ans.push([c, ids[d.get(c)!]]);
210+
}
211+
}
212+
return ans;
213+
}
204214
```
205215

206216
### **...**

solution/2400-2499/2456.Most Popular Video Creator/README_EN.md

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -61,53 +61,40 @@ Since &quot;b&quot; is lexicographically smaller than &quot;c&quot;, it is inclu
6161
class Solution:
6262
def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
6363
cnt = defaultdict(int)
64-
d = {}
65-
x = {}
66-
for c, i, v in zip(creators, ids, views):
64+
d = defaultdict(int)
65+
for k, (c, i, v) in enumerate(zip(creators, ids, views)):
6766
cnt[c] += v
68-
if c not in d or d[c] < v or (d[c] == v and x[c] > i):
69-
d[c], x[c] = v, i
70-
ans = []
71-
pre = -1
72-
for a, b in cnt.items():
73-
if b > pre:
74-
ans = [[a, x[a]]]
75-
pre = b
76-
elif b == pre:
77-
ans.append([a, x[a]])
78-
return ans
67+
if c not in d or views[d[c]] < v or (views[d[c]] == v and ids[d[c]] > i):
68+
d[c] = k
69+
mx = max(cnt.values())
70+
return [[c, ids[d[c]]] for c, x in cnt.items() if x == mx]
7971
```
8072

8173
### **Java**
8274

8375
```java
8476
class Solution {
8577
public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
86-
Map<String, Integer> cnt = new HashMap<>();
87-
Map<String, Integer> d = new HashMap<>();
88-
Map<String, String> x = new HashMap<>();
8978
int n = ids.length;
79+
Map<String, Long> cnt = new HashMap<>(n);
80+
Map<String, Integer> d = new HashMap<>(n);
9081
for (int k = 0; k < n; ++k) {
91-
var c = creators[k];
92-
var i = ids[k];
93-
int v = views[k];
94-
cnt.put(c, cnt.getOrDefault(c, 0) + v);
95-
if (!d.containsKey(c) || d.get(c) < v || (d.get(c) == v && x.get(c).compareTo(i) > 0)) {
96-
d.put(c, v);
97-
x.put(c, i);
82+
String c = creators[k], i = ids[k];
83+
long v = views[k];
84+
cnt.merge(c, v, Long::sum);
85+
if (!d.containsKey(c) || views[d.get(c)] < v || (views[d.get(c)] == v && ids[d.get(c)].compareTo(i) > 0)) {
86+
d.put(c, k);
9887
}
9988
}
89+
long mx = 0;
90+
for (long x : cnt.values()) {
91+
mx = Math.max(mx, x);
92+
}
10093
List<List<String>> ans = new ArrayList<>();
101-
int pre = -1;
10294
for (var e : cnt.entrySet()) {
103-
String a = e.getKey();
104-
int b = e.getValue();
105-
if (b > pre) {
106-
ans.clear();
107-
ans.add(Arrays.asList(a, x.get(a)));
108-
pre = b;
109-
} else if (b == pre) {
110-
ans.add(Arrays.asList(a, x.get(a)));
95+
if (e.getValue() == mx) {
96+
String c = e.getKey();
97+
ans.add(List.of(c, ids[d.get(c)]));
11198
}
11299
}
113100
return ans;
@@ -121,29 +108,25 @@ class Solution {
121108
class Solution {
122109
public:
123110
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
124-
unordered_map<string, long> cnt;
111+
unordered_map<string, long long> cnt;
125112
unordered_map<string, int> d;
126-
unordered_map<string, string> x;
127113
int n = ids.size();
128114
for (int k = 0; k < n; ++k) {
129-
auto c = creators[k];
130-
auto i = ids[k];
115+
auto c = creators[k], id = ids[k];
131116
int v = views[k];
132117
cnt[c] += v;
133-
if (!d.count(c) || d[c] < v || (d[c] == v && x[c] > i)) {
134-
d[c] = v;
135-
x[c] = i;
118+
if (!d.count(c) || views[d[c]] < v || (views[d[c]] == v && ids[d[c]] > id)) {
119+
d[c] = k;
136120
}
137121
}
138-
long pre = -1;
122+
long long mx = 0;
123+
for (auto& [_, x] : cnt) {
124+
mx = max(mx, x);
125+
}
139126
vector<vector<string>> ans;
140-
for (auto& [a, b] : cnt) {
141-
if (b > pre) {
142-
ans.clear();
143-
ans.push_back({a, x[a]});
144-
pre = b;
145-
} else if (b == pre) {
146-
ans.push_back({a, x[a]});
127+
for (auto& [c, x] : cnt) {
128+
if (x == mx) {
129+
ans.push_back({c, ids[d[c]]});
147130
}
148131
}
149132
return ans;
@@ -157,32 +140,59 @@ public:
157140
func mostPopularCreator(creators []string, ids []string, views []int) (ans [][]string) {
158141
cnt := map[string]int{}
159142
d := map[string]int{}
160-
x := map[string]string{}
161143
for k, c := range creators {
162144
i, v := ids[k], views[k]
163145
cnt[c] += v
164-
if t, ok := d[c]; !ok || t < v || (t == v && x[c] > i) {
165-
d[c] = v
166-
x[c] = i
146+
if j, ok := d[c]; !ok || views[j] < v || (views[j] == v && ids[j] > i) {
147+
d[c] = k
167148
}
168149
}
169-
pre := -1
170-
for a, b := range cnt {
171-
if b > pre {
172-
ans = [][]string{[]string{a, x[a]}}
173-
pre = b
174-
} else if b == pre {
175-
ans = append(ans, []string{a, x[a]})
150+
mx := 0
151+
for _, x := range cnt {
152+
if mx < x {
153+
mx = x
176154
}
177155
}
178-
return ans
156+
for c, x := range cnt {
157+
if x == mx {
158+
ans = append(ans, []string{c, ids[d[c]]})
159+
}
160+
}
161+
return
179162
}
180163
```
181164

182165
### **TypeScript**
183166

184167
```ts
185-
168+
function mostPopularCreator(
169+
creators: string[],
170+
ids: string[],
171+
views: number[],
172+
): string[][] {
173+
const cnt: Map<string, number> = new Map();
174+
const d: Map<string, number> = new Map();
175+
const n = ids.length;
176+
for (let k = 0; k < n; ++k) {
177+
const [c, i, v] = [creators[k], ids[k], views[k]];
178+
cnt.set(c, (cnt.get(c) ?? 0) + v);
179+
if (
180+
!d.has(c) ||
181+
views[d.get(c)!] < v ||
182+
(views[d.get(c)!] === v && ids[d.get(c)!] > i)
183+
) {
184+
d.set(c, k);
185+
}
186+
}
187+
const mx = Math.max(...cnt.values());
188+
const ans: string[][] = [];
189+
for (const [c, x] of cnt) {
190+
if (x === mx) {
191+
ans.push([c, ids[d.get(c)!]]);
192+
}
193+
}
194+
return ans;
195+
}
186196
```
187197

188198
### **...**

0 commit comments

Comments
 (0)