Skip to content

Commit d8fd6a9

Browse files
authored
feat: update lc problems (#3414)
1 parent 29871c1 commit d8fd6a9

File tree

33 files changed

+771
-83
lines changed

33 files changed

+771
-83
lines changed

solution/0800-0899/0813.Largest Sum of Averages/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ tags:
3131
<pre>
3232
<strong>输入:</strong> nums = [9,1,2,3,9], k = 3
3333
<strong>输出:</strong> 20.00000
34-
<strong>解释:</strong>
35-
nums 的最优分组是[9], [1, 2, 3], [9]. 得到的分数是 9 + (1 + 2 + 3) / 3 + 9 = 20.
36-
我们也可以把 nums 分成[9, 1], [2], [3, 9].
34+
<strong>解释:</strong>
35+
nums 的最优分组是[9], [1, 2, 3], [9]. 得到的分数是 9 + (1 + 2 + 3) / 3 + 9 = 20.
36+
我们也可以把 nums 分成[9, 1], [2], [3, 9].
3737
这样的分组得到的分数为 5 + 2 + 6 = 13, 但不是最大值.
3838
</pre>
3939

solution/0800-0899/0813.Largest Sum of Averages/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> nums = [9,1,2,3,9], k = 3
3232
<strong>Output:</strong> 20.00000
33-
<strong>Explanation:</strong>
33+
<strong>Explanation:</strong>
3434
The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
3535
We could have also partitioned nums into [9, 1], [2], [3, 9], for example.
3636
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.

solution/0800-0899/0814.Binary Tree Pruning/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tags:
2828
<pre>
2929
<strong>Input:</strong> root = [1,null,0,0,1]
3030
<strong>Output:</strong> [1,null,0,null,1]
31-
<strong>Explanation:</strong>
31+
<strong>Explanation:</strong>
3232
Only the red nodes satisfy the property &quot;every subtree not containing a 1&quot;.
3333
The diagram on the right represents the answer.
3434
</pre>

solution/0800-0899/0849.Maximize Distance to Closest Person/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tags:
3434
<strong>解释:
3535
</strong>如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。
3636
如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。
37-
因此,他到离他最近的人的最大距离是 2 。
37+
因此,他到离他最近的人的最大距离是 2 。
3838
</pre>
3939

4040
<p><strong>示例 2:</strong></p>

solution/1300-1399/1310.XOR Queries of a Subarray/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ tags:
3232

3333
<pre>
3434
<strong>输入:</strong>arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
35-
<strong>输出:</strong>[2,7,14,8]
35+
<strong>输出:</strong>[2,7,14,8]
3636
<strong>解释:</strong>
3737
数组中元素的二进制表示形式是:
38-
1 = 0001
39-
3 = 0011
40-
4 = 0100
41-
8 = 1000
38+
1 = 0001
39+
3 = 0011
40+
4 = 0100
41+
8 = 1000
4242
查询的 XOR 值为:
43-
[0,1] = 1 xor 3 = 2
44-
[1,2] = 3 xor 4 = 7
45-
[0,3] = 1 xor 3 xor 4 xor 8 = 14
43+
[0,1] = 1 xor 3 = 2
44+
[1,2] = 3 xor 4 = 7
45+
[0,3] = 1 xor 3 xor 4 xor 8 = 14
4646
[3,3] = 8
4747
</pre>
4848

solution/1300-1399/1310.XOR Queries of a Subarray/README_EN.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ tags:
3131

3232
<pre>
3333
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
34-
<strong>Output:</strong> [2,7,14,8]
35-
<strong>Explanation:</strong>
34+
<strong>Output:</strong> [2,7,14,8]
35+
<strong>Explanation:</strong>
3636
The binary representation of the elements in the array are:
37-
1 = 0001
38-
3 = 0011
39-
4 = 0100
40-
8 = 1000
37+
1 = 0001
38+
3 = 0011
39+
4 = 0100
40+
8 = 1000
4141
The XOR values for queries are:
42-
[0,1] = 1 xor 3 = 2
43-
[1,2] = 3 xor 4 = 7
44-
[0,3] = 1 xor 3 xor 4 xor 8 = 14
42+
[0,1] = 1 xor 3 = 2
43+
[1,2] = 3 xor 4 = 7
44+
[0,3] = 1 xor 3 xor 4 xor 8 = 14
4545
[3,3] = 8
4646
</pre>
4747

solution/1300-1399/1311.Get Watched Videos by Your Friends/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1311.Get%20Watched%20Videos%20by%20Your%20Friends/images/leetcode_friends_1.png" style="height: 179px; width: 129px;"></strong></p>
3838

3939
<pre><strong>输入:</strong>watchedVideos = [[&quot;A&quot;,&quot;B&quot;],[&quot;C&quot;],[&quot;B&quot;,&quot;C&quot;],[&quot;D&quot;]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
40-
<strong>输出:</strong>[&quot;B&quot;,&quot;C&quot;]
40+
<strong>输出:</strong>[&quot;B&quot;,&quot;C&quot;]
4141
<strong>解释:</strong>
4242
你的 id 为 0(绿色),你的朋友包括(黄色):
4343
id 为 1 -&gt; watchedVideos = [&quot;C&quot;]&nbsp;

solution/1300-1399/1311.Get Watched Videos by Your Friends/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ tags:
3333

3434
<pre>
3535
<strong>Input:</strong> watchedVideos = [[&quot;A&quot;,&quot;B&quot;],[&quot;C&quot;],[&quot;B&quot;,&quot;C&quot;],[&quot;D&quot;]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
36-
<strong>Output:</strong> [&quot;B&quot;,&quot;C&quot;]
37-
<strong>Explanation:</strong>
36+
<strong>Output:</strong> [&quot;B&quot;,&quot;C&quot;]
37+
<strong>Explanation:</strong>
3838
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
3939
Person with id = 1 -&gt; watchedVideos = [&quot;C&quot;]&nbsp;
4040
Person with id = 2 -&gt; watchedVideos = [&quot;B&quot;,&quot;C&quot;]&nbsp;
@@ -50,7 +50,7 @@ C -&gt; 2
5050
<pre>
5151
<strong>Input:</strong> watchedVideos = [[&quot;A&quot;,&quot;B&quot;],[&quot;C&quot;],[&quot;B&quot;,&quot;C&quot;],[&quot;D&quot;]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
5252
<strong>Output:</strong> [&quot;D&quot;]
53-
<strong>Explanation:</strong>
53+
<strong>Explanation:</strong>
5454
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
5555
</pre>
5656

solution/2000-2099/2029.Stone Game IX/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tags:
4242
<strong>输出:</strong>true
4343
<strong>解释:</strong>游戏进行如下:
4444
- 回合 1:Alice 可以移除任意一个石子。
45-
- 回合 2:Bob 移除剩下的石子。
45+
- 回合 2:Bob 移除剩下的石子。
4646
已移除的石子的值总和为 1 + 2 = 3 且可以被 3 整除。因此,Bob 输,Alice 获胜。
4747
</pre>
4848

@@ -51,7 +51,7 @@ tags:
5151
<pre>
5252
<strong>输入:</strong>stones = [2]
5353
<strong>输出:</strong>false
54-
<strong>解释:</strong>Alice 会移除唯一一个石子,已移除石子的值总和为 2 。
54+
<strong>解释:</strong>Alice 会移除唯一一个石子,已移除石子的值总和为 2 。
5555
由于所有石子都已移除,且值总和无法被 3 整除,Bob 获胜。
5656
</pre>
5757

solution/2000-2099/2029.Stone Game IX/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tags:
3636
<strong>Output:</strong> true
3737
<strong>Explanation:</strong>&nbsp;The game will be played as follows:
3838
- Turn 1: Alice can remove either stone.
39-
- Turn 2: Bob removes the remaining stone.
39+
- Turn 2: Bob removes the remaining stone.
4040
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.
4141
</pre>
4242

@@ -45,7 +45,7 @@ The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob
4545
<pre>
4646
<strong>Input:</strong> stones = [2]
4747
<strong>Output:</strong> false
48-
<strong>Explanation:</strong>&nbsp;Alice will remove the only stone, and the sum of the values on the removed stones is 2.
48+
<strong>Explanation:</strong>&nbsp;Alice will remove the only stone, and the sum of the values on the removed stones is 2.
4949
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.
5050
</pre>
5151

solution/2800-2899/2800.Shortest String That Contains Three Strings/README.md

+226
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,230 @@ impl Solution {
288288

289289
<!-- solution:end -->
290290

291+
<!-- solution:start -->
292+
293+
### 方法二:枚举 + KMP
294+
295+
我们可以使用 KMP 算法来优化字符串的合并过程。
296+
297+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是三个字符串的长度之和。
298+
299+
<!-- tabs:start -->
300+
301+
#### Python3
302+
303+
```python
304+
class Solution:
305+
def minimumString(self, a: str, b: str, c: str) -> str:
306+
def f(s: str, t: str) -> str:
307+
if s in t:
308+
return t
309+
if t in s:
310+
return s
311+
p = t + "#" + s + "$"
312+
n = len(p)
313+
next = [0] * n
314+
next[0] = -1
315+
i, j = 2, 0
316+
while i < n:
317+
if p[i - 1] == p[j]:
318+
j += 1
319+
next[i] = j
320+
i += 1
321+
elif j:
322+
j = next[j]
323+
else:
324+
next[i] = 0
325+
i += 1
326+
return s + t[next[-1] :]
327+
328+
ans = ""
329+
for a, b, c in permutations((a, b, c)):
330+
s = f(f(a, b), c)
331+
if ans == "" or len(s) < len(ans) or (len(s) == len(ans) and s < ans):
332+
ans = s
333+
return ans
334+
```
335+
336+
#### Java
337+
338+
```java
339+
class Solution {
340+
public String minimumString(String a, String b, String c) {
341+
String[] s = {a, b, c};
342+
int[][] perm = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 1, 0}, {2, 0, 1}};
343+
String ans = "";
344+
for (var p : perm) {
345+
int i = p[0], j = p[1], k = p[2];
346+
String t = f(f(s[i], s[j]), s[k]);
347+
if ("".equals(ans) || t.length() < ans.length()
348+
|| (t.length() == ans.length() && t.compareTo(ans) < 0)) {
349+
ans = t;
350+
}
351+
}
352+
return ans;
353+
}
354+
355+
private String f(String s, String t) {
356+
if (s.contains(t)) {
357+
return s;
358+
}
359+
if (t.contains(s)) {
360+
return t;
361+
}
362+
char[] p = (t + "#" + s + "$").toCharArray();
363+
int n = p.length;
364+
int[] next = new int[n];
365+
next[0] = -1;
366+
for (int i = 2, j = 0; i < n;) {
367+
if (p[i - 1] == p[j]) {
368+
next[i++] = ++j;
369+
} else if (j > 0) {
370+
j = next[j];
371+
} else {
372+
next[i++] = 0;
373+
}
374+
}
375+
return s + t.substring(next[n - 1]);
376+
}
377+
}
378+
```
379+
380+
#### C++
381+
382+
```cpp
383+
class Solution {
384+
public:
385+
string minimumString(string a, string b, string c) {
386+
vector<string> s = {a, b, c};
387+
vector<vector<int>> perm = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 1, 0}, {2, 0, 1}};
388+
string ans = "";
389+
for (auto& p : perm) {
390+
int i = p[0], j = p[1], k = p[2];
391+
string t = f(f(s[i], s[j]), s[k]);
392+
if (ans == "" || t.size() < ans.size() || (t.size() == ans.size() && t < ans)) {
393+
ans = t;
394+
}
395+
}
396+
return ans;
397+
}
398+
399+
string f(string s, string t) {
400+
if (s.find(t) != string::npos) {
401+
return s;
402+
}
403+
if (t.find(s) != string::npos) {
404+
return t;
405+
}
406+
string p = t + "#" + s + "$";
407+
int n = p.size();
408+
int next[n];
409+
next[0] = -1;
410+
next[1] = 0;
411+
for (int i = 2, j = 0; i < n;) {
412+
if (p[i - 1] == p[j]) {
413+
next[i++] = ++j;
414+
} else if (j > 0) {
415+
j = next[j];
416+
} else {
417+
next[i++] = 0;
418+
}
419+
}
420+
return s + t.substr(next[n - 1]);
421+
};
422+
};
423+
```
424+
425+
#### Go
426+
427+
```go
428+
func minimumString(a string, b string, c string) string {
429+
f := func(s, t string) string {
430+
if strings.Contains(s, t) {
431+
return s
432+
}
433+
if strings.Contains(t, s) {
434+
return t
435+
}
436+
p := t + "#" + s + "$"
437+
n := len(p)
438+
next := make([]int, n)
439+
next[0] = -1
440+
for i, j := 2, 0; i < n; {
441+
if p[i-1] == p[j] {
442+
j++
443+
next[i] = j
444+
i++
445+
} else if j > 0 {
446+
j = next[j]
447+
} else {
448+
next[i] = 0
449+
i++
450+
}
451+
}
452+
return s + t[next[n-1]:]
453+
}
454+
s := [3]string{a, b, c}
455+
ans := ""
456+
for _, p := range [][]int{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}} {
457+
i, j, k := p[0], p[1], p[2]
458+
t := f(f(s[i], s[j]), s[k])
459+
if ans == "" || len(t) < len(ans) || (len(t) == len(ans) && t < ans) {
460+
ans = t
461+
}
462+
}
463+
return ans
464+
}
465+
```
466+
467+
#### TypeScript
468+
469+
```ts
470+
function minimumString(a: string, b: string, c: string): string {
471+
const f = (s: string, t: string): string => {
472+
if (s.includes(t)) {
473+
return s;
474+
}
475+
if (t.includes(s)) {
476+
return t;
477+
}
478+
const p = t + '#' + s + '$';
479+
const n = p.length;
480+
const next: number[] = Array(n).fill(0);
481+
next[0] = -1;
482+
for (let i = 2, j = 0; i < n; ) {
483+
if (p[i - 1] === p[j]) {
484+
next[i++] = ++j;
485+
} else if (j) {
486+
j = next[j];
487+
} else {
488+
next[i++] = 0;
489+
}
490+
}
491+
return s + t.slice(next[n - 1]);
492+
};
493+
const s: string[] = [a, b, c];
494+
const perm: number[][] = [
495+
[0, 1, 2],
496+
[0, 2, 1],
497+
[1, 0, 2],
498+
[1, 2, 0],
499+
[2, 0, 1],
500+
[2, 1, 0],
501+
];
502+
let ans = '';
503+
for (const [i, j, k] of perm) {
504+
const t = f(f(s[i], s[j]), s[k]);
505+
if (ans === '' || t.length < ans.length || (t.length === ans.length && t < ans)) {
506+
ans = t;
507+
}
508+
}
509+
return ans;
510+
}
511+
```
512+
513+
<!-- tabs:end -->
514+
515+
<!-- solution:end -->
516+
291517
<!-- problem:end -->

0 commit comments

Comments
 (0)