Skip to content

Commit cde2ec7

Browse files
committed
feat: add solutions to lc problems: No.2451~2456
* No.2451.Odd String Difference * No.2452.Words Within Two Edits of Dictionary * No.2453.Destroy Sequential Targets * No.2454.Next Greater Element IV * No.2456.Most Popular Video Creator
1 parent 0168c94 commit cde2ec7

File tree

10 files changed

+341
-40
lines changed

10 files changed

+341
-40
lines changed

solution/2400-2499/2451.Odd String Difference/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:哈希表模拟**
59+
60+
我们用哈希表 `cnt` 存放每个差值数组以及其对应的字符串列表,然后遍历每个字符串列表,找到其中长度为 $1$ 的列表,返回对应的元素即可。
61+
62+
时间复杂度 $O(m\times n)$,其中 $m$ 和 $n$ 分别为字符串数组 `words` 的长度和字符串的平均长度。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**

solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:暴力枚举**
54+
55+
遍历 `queries` 中的每个单词,对于每个单词,遍历 `dictionary` 中的每个单词,判断两个单词不同字符的位置数是否小于 $3$,如果是,则将该单词加入结果集。
56+
57+
时间复杂度 $O(m\times n\times k)$。其中 $m$ 和 $n$ 分别是 `queries``dictionary` 的长度,而 $k$ 是 `queries``dictionary` 中单词的长度。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**

solution/2400-2499/2453.Destroy Sequential Targets/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:取模 + 枚举**
57+
58+
我们遍历数组 `nums`,用哈希表 `cnt` 统计每个数模 `space` 后的余数出现的次数。次数越多,意味着可以摧毁的目标越多。我们找到最多次数的组,取组中的最小值即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**

solution/2400-2499/2454.Next Greater Element IV/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464

6565
**方法一:单调栈 + 优先队列(小根堆)**
6666

67+
求下一个更大的元素,可以使用单调栈来实现。我们维护一个栈,然后从左到右遍历数组,如果栈顶元素小于当前元素,则当前元素就是栈顶元素的下一个更大的元素。
68+
69+
这道题的变形是求下一个更大的元素的下一个更大的元素,即第二大的元素。我们观察单调栈求下一个更大元素的过程,每次出栈时,栈顶元素找到了下一个更大的元素,但我们是要为栈顶元素找到第二个更大的元素。次数,我们可以将栈顶元素出栈,放到一个优先队列(小根堆)中。每次遍历数组元素时,先判断当前元素是否大于优先队列的堆顶元素,如果大于,说明堆顶元素找打了第二个更大的元素,更新答案数组,然后弹出堆顶元素,继续判断当前元素是否大于优先队列的堆顶元素,直到堆为空或者当前元素不大于堆顶元素。
70+
71+
接着,执行单调栈的相关操作,弹出栈顶元素后,放入到优先队列中。
72+
73+
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
74+
6775
<!-- tabs:start -->
6876

6977
### **Python3**

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

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

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

64+
我们用哈希表 `cnt` 统计每个创作者的视频播放量总和,用哈希表 `d``x` 记录每个创作者的最大播放量和对应的视频 `id`
65+
66+
然后遍历哈希表 `cnt`,找到最大视频播放量总和的创作者,将其对应的视频 `id` 加入答案数组 `ans` 中。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为视频数量。
69+
6470
<!-- tabs:start -->
6571

6672
### **Python3**
@@ -70,33 +76,125 @@ id 为 "b" 和 "c" 的视频都满足播放量最高的条件。
7076
```python
7177
class Solution:
7278
def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
73-
ans = []
74-
cnt = Counter()
75-
d = defaultdict(int)
76-
x = defaultdict(str)
77-
for c, idx, view in zip(creators, ids, views):
78-
cnt[c] += view
79-
if c not in d or d[c] < view or (d[c] == view and x[c] > idx):
80-
d[c] = view
81-
x[c] = idx
79+
cnt = defaultdict(int)
80+
d = {}
81+
x = {}
82+
for c, i, v in zip(creators, ids, views):
83+
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
8286
ans = []
8387
pre = -1
84-
for a, b in cnt.most_common():
85-
if b >= pre:
88+
for a, b in cnt.items():
89+
if b > pre:
90+
ans = [[a, x[a]]]
8691
pre = b
92+
elif b == pre:
8793
ans.append([a, x[a]])
88-
else:
89-
break
9094
return ans
91-
9295
```
9396

9497
### **Java**
9598

9699
<!-- 这里可写当前语言的特殊实现逻辑 -->
97100

98101
```java
102+
class Solution {
103+
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<>();
107+
int n = ids.length;
108+
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);
116+
}
117+
}
118+
List<List<String>> ans = new ArrayList<>();
119+
int pre = -1;
120+
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)));
129+
}
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
142+
unordered_map<string, long> cnt;
143+
unordered_map<string, int> d;
144+
unordered_map<string, string> x;
145+
int n = ids.size();
146+
for (int k = 0; k < n; ++k) {
147+
auto c = creators[k];
148+
auto i = ids[k];
149+
int v = views[k];
150+
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;
154+
}
155+
}
156+
long pre = -1;
157+
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]});
165+
}
166+
}
167+
return ans;
168+
}
169+
};
170+
```
99171
172+
### **Go**
173+
174+
```go
175+
func mostPopularCreator(creators []string, ids []string, views []int) (ans [][]string) {
176+
cnt := map[string]int{}
177+
d := map[string]int{}
178+
x := map[string]string{}
179+
for k, c := range creators {
180+
i, v := ids[k], views[k]
181+
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
185+
}
186+
}
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]})
194+
}
195+
}
196+
return ans
197+
}
100198
```
101199

102200
### **TypeScript**

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

Lines changed: 106 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,123 @@ Since &quot;b&quot; is lexicographically smaller than &quot;c&quot;, it is inclu
6060
```python
6161
class Solution:
6262
def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
63-
ans = []
64-
cnt = Counter()
65-
d = defaultdict(int)
66-
x = defaultdict(str)
67-
for c, idx, view in zip(creators, ids, views):
68-
cnt[c] += view
69-
if c not in d or d[c] < view or (d[c] == view and x[c] > idx):
70-
d[c] = view
71-
x[c] = idx
63+
cnt = defaultdict(int)
64+
d = {}
65+
x = {}
66+
for c, i, v in zip(creators, ids, views):
67+
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
7270
ans = []
7371
pre = -1
74-
for a, b in cnt.most_common():
75-
if b >= pre:
72+
for a, b in cnt.items():
73+
if b > pre:
74+
ans = [[a, x[a]]]
7675
pre = b
76+
elif b == pre:
7777
ans.append([a, x[a]])
78-
else:
79-
break
8078
return ans
8179
```
8280

8381
### **Java**
8482

8583
```java
84+
class Solution {
85+
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<>();
89+
int n = ids.length;
90+
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);
98+
}
99+
}
100+
List<List<String>> ans = new ArrayList<>();
101+
int pre = -1;
102+
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)));
111+
}
112+
}
113+
return ans;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
124+
unordered_map<string, long> cnt;
125+
unordered_map<string, int> d;
126+
unordered_map<string, string> x;
127+
int n = ids.size();
128+
for (int k = 0; k < n; ++k) {
129+
auto c = creators[k];
130+
auto i = ids[k];
131+
int v = views[k];
132+
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;
136+
}
137+
}
138+
long pre = -1;
139+
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]});
147+
}
148+
}
149+
return ans;
150+
}
151+
};
152+
```
86153
154+
### **Go**
155+
156+
```go
157+
func mostPopularCreator(creators []string, ids []string, views []int) (ans [][]string) {
158+
cnt := map[string]int{}
159+
d := map[string]int{}
160+
x := map[string]string{}
161+
for k, c := range creators {
162+
i, v := ids[k], views[k]
163+
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
167+
}
168+
}
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]})
176+
}
177+
}
178+
return ans
179+
}
87180
```
88181

89182
### **TypeScript**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
4+
unordered_map<string, long> cnt;
5+
unordered_map<string, int> d;
6+
unordered_map<string, string> x;
7+
int n = ids.size();
8+
for (int k = 0; k < n; ++k) {
9+
auto c = creators[k];
10+
auto i = ids[k];
11+
int v = views[k];
12+
cnt[c] += v;
13+
if (!d.count(c) || d[c] < v || (d[c] == v && x[c] > i)) {
14+
d[c] = v;
15+
x[c] = i;
16+
}
17+
}
18+
long pre = -1;
19+
vector<vector<string>> ans;
20+
for (auto& [a, b] : cnt) {
21+
if (b > pre) {
22+
ans.clear();
23+
ans.push_back({a, x[a]});
24+
pre = b;
25+
} else if (b == pre) {
26+
ans.push_back({a, x[a]});
27+
}
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)