Skip to content

Commit 5ef355b

Browse files
authored
Merge branch 'main' into main
2 parents cf601f4 + 65f3542 commit 5ef355b

File tree

29 files changed

+292
-41
lines changed

29 files changed

+292
-41
lines changed

solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ tags:
8888

8989
- 如果 $c$ 是 "a",由于要先删除 "ab",因此此时我们不消除该字符,只增加 $\textit{cnt1}$;
9090
- 如果 $c$ 是 "b",如果此时 $\textit{cnt1} > 0$,我们可以消除一个 "ab",并增加 $x$ 分,否则我们只能增加 $\textit{cnt2}$;
91-
- 如果 $c$ 是其他字符,那么对于该子字符串,我们剩下了一个 $\textit{cnt2}$ 个 "b" 和 $\textit{cnt1}$ 个 "a",我们可以消除 $\min(\textit{cnt1}, \textit{cnt2})$ 个 "ab",并增加 $y$ 分。
91+
- 如果 $c$ 是其他字符,那么对于该子字符串,我们剩下了 $\textit{cnt2}$ 个 "b" 和 $\textit{cnt1}$ 个 "a",我们可以消除 $\min(\textit{cnt1}, \textit{cnt2})$ 个 "ba",并增加若干个 $y$ 分。
9292

93-
遍历结束后,我们还需要额外处理一下剩余的 "ab",增加若干个 $y$ 分。
93+
遍历结束后,我们还需要额外处理一下剩余的 "ba",增加若干个 $y$ 分。
9494

9595
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
9696

solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ Total score = 5 + 4 + 5 + 5 = 19.</pre>
7676

7777
### Solution 1: Greedy
7878

79-
Let's assume that the score of the substring "ab" is always not lower than the score of the substring "ba". If not, we can swap "a" and "b", and simultaneously swap $x$ and $y$.
79+
We can assume that the score of substring "ab" is always no less than the score of substring "ba". If not, we can swap "a" and "b", and simultaneously swap $x$ and $y$.
8080

81-
Next, we only need to consider the case where the string contains only "a" and "b". If the string contains other characters, we can treat them as a dividing point, splitting the string into several substrings that contain only "a" and "b", and then calculate the score for each substring separately.
81+
Next, we only need to consider the case where the string contains only "a" and "b". If the string contains other characters, we can treat them as split points, dividing the string into several substrings that contain only "a" and "b", and then calculate the score for each substring separately.
8282

83-
We observe that, for a substring containing only "a" and "b", no matter what operations are taken, in the end, there will only be one type of character left, or an empty string. Since each operation will delete one "a" and one "b" simultaneously, the total number of operations is fixed. We can greedily delete "ab" first, then "ba", to ensure the maximum score.
83+
We observe that for a substring containing only "a" and "b", no matter what operations we take, we will eventually be left with only one type of character, or an empty string. Since each operation removes one "a" and one "b" simultaneously, the total number of operations is fixed. We can greedily remove "ab" first, then remove "ba", which ensures the maximum score.
8484

85-
Therefore, we can use two variables $\textit{cnt1}$ and $\textit{cnt2}$ to record the number of "a" and "b", respectively. Then, we traverse the string, update $\textit{cnt1}$ and $\textit{cnt2}$ based on the current character, and calculate the score.
85+
Therefore, we can use two variables $\textit{cnt1}$ and $\textit{cnt2}$ to record the counts of "a" and "b" respectively, then traverse the string and update $\textit{cnt1}$ and $\textit{cnt2}$ according to different cases of the current character, while calculating the score.
8686

87-
For the current character $c$:
87+
For the current character $c$ being traversed:
8888

89-
- If $c$ is "a", since we need to delete "ab" first, we do not eliminate this character at this time, only increase $\textit{cnt1}$;
90-
- If $c$ is "b", if $\textit{cnt1} > 0$ at this time, we can eliminate an "ab" and add $x$ points; otherwise, we can only increase $\textit{cnt2}$;
91-
- If $c$ is another character, then for this substring, we are left with $\textit{cnt2}$ "b" and $\textit{cnt1}$ "a", we can eliminate $\min(\textit{cnt1}, \textit{cnt2})$ "ab" and add $y$ points.
89+
- If $c$ is "a", since we want to remove "ab" first, we don't eliminate this character at this time, only increment $\textit{cnt1}$;
90+
- If $c$ is "b", if $\textit{cnt1} > 0$ at this time, we can eliminate one "ab" and add $x$ points; otherwise, we can only increment $\textit{cnt2}$;
91+
- If $c$ is another character, then for this substring, we have $\textit{cnt2}$ "b"s and $\textit{cnt1}$ "a"s left. We can eliminate $\min(\textit{cnt1}, \textit{cnt2})$ "ba"s and add several $y$ points.
9292

93-
After the traversal is finished, we also need to additionally handle the remaining "ab", adding several $y$ points.
93+
After traversal, we need to handle the remaining "ba"s and add several $y$ points.
9494

95-
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
95+
The time complexity is $O(n)$, where $n$ is the length of string $s$. The space complexity is $O(1)$.
9696

9797
<!-- tabs:start -->
9898

solution/1900-1999/1948.Delete Duplicate Folders in System/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ folder named &quot;b&quot;.
6868
<pre>
6969
<strong>Input:</strong> paths = [[&quot;a&quot;],[&quot;c&quot;],[&quot;a&quot;,&quot;b&quot;],[&quot;c&quot;,&quot;b&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;x&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;x&quot;,&quot;y&quot;],[&quot;w&quot;],[&quot;w&quot;,&quot;y&quot;]]
7070
<strong>Output:</strong> [[&quot;c&quot;],[&quot;c&quot;,&quot;b&quot;],[&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;]]
71-
<strong>Explanation: </strong>The file structure is as shown.
71+
<strong>Explanation: </strong>The file structure is as shown.
7272
Folders &quot;/a/b/x&quot; and &quot;/w&quot; (and their subfolders) are marked for deletion because they both contain an empty folder named &quot;y&quot;.
7373
Note that folders &quot;/a&quot; and &quot;/c&quot; are identical after the deletion, but they are not deleted because they were not marked beforehand.
7474
</pre>

solution/2300-2399/2322.Minimum Score After Removals on a Tree/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,66 @@ impl Solution {
399399
}
400400
```
401401

402+
#### C#
403+
404+
```cs
405+
public class Solution {
406+
public int MinimumScore(int[] nums, int[][] edges) {
407+
int n = nums.Length;
408+
List<int>[] g = new List<int>[n];
409+
for (int i = 0; i < n; i++) {
410+
g[i] = new List<int>();
411+
}
412+
foreach (var e in edges) {
413+
int a = e[0], b = e[1];
414+
g[a].Add(b);
415+
g[b].Add(a);
416+
}
417+
418+
int s = 0;
419+
foreach (int x in nums) {
420+
s ^= x;
421+
}
422+
423+
int ans = int.MaxValue;
424+
int s1 = 0;
425+
426+
int Dfs(int i, int fa) {
427+
int res = nums[i];
428+
foreach (int j in g[i]) {
429+
if (j != fa) {
430+
res ^= Dfs(j, i);
431+
}
432+
}
433+
return res;
434+
}
435+
436+
int Dfs2(int i, int fa) {
437+
int res = nums[i];
438+
foreach (int j in g[i]) {
439+
if (j != fa) {
440+
int s2 = Dfs2(j, i);
441+
res ^= s2;
442+
int mx = Math.Max(Math.Max(s ^ s1, s2), s1 ^ s2);
443+
int mn = Math.Min(Math.Min(s ^ s1, s2), s1 ^ s2);
444+
ans = Math.Min(ans, mx - mn);
445+
}
446+
}
447+
return res;
448+
}
449+
450+
for (int i = 0; i < n; ++i) {
451+
foreach (int j in g[i]) {
452+
s1 = Dfs(i, j);
453+
Dfs2(i, j);
454+
}
455+
}
456+
457+
return ans;
458+
}
459+
}
460+
```
461+
402462
<!-- tabs:end -->
403463

404464
<!-- solution:end -->

solution/2300-2399/2322.Minimum Score After Removals on a Tree/README_EN.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,66 @@ impl Solution {
399399
}
400400
```
401401

402+
#### C#
403+
404+
```cs
405+
public class Solution {
406+
public int MinimumScore(int[] nums, int[][] edges) {
407+
int n = nums.Length;
408+
List<int>[] g = new List<int>[n];
409+
for (int i = 0; i < n; i++) {
410+
g[i] = new List<int>();
411+
}
412+
foreach (var e in edges) {
413+
int a = e[0], b = e[1];
414+
g[a].Add(b);
415+
g[b].Add(a);
416+
}
417+
418+
int s = 0;
419+
foreach (int x in nums) {
420+
s ^= x;
421+
}
422+
423+
int ans = int.MaxValue;
424+
int s1 = 0;
425+
426+
int Dfs(int i, int fa) {
427+
int res = nums[i];
428+
foreach (int j in g[i]) {
429+
if (j != fa) {
430+
res ^= Dfs(j, i);
431+
}
432+
}
433+
return res;
434+
}
435+
436+
int Dfs2(int i, int fa) {
437+
int res = nums[i];
438+
foreach (int j in g[i]) {
439+
if (j != fa) {
440+
int s2 = Dfs2(j, i);
441+
res ^= s2;
442+
int mx = Math.Max(Math.Max(s ^ s1, s2), s1 ^ s2);
443+
int mn = Math.Min(Math.Min(s ^ s1, s2), s1 ^ s2);
444+
ans = Math.Min(ans, mx - mn);
445+
}
446+
}
447+
return res;
448+
}
449+
450+
for (int i = 0; i < n; ++i) {
451+
foreach (int j in g[i]) {
452+
s1 = Dfs(i, j);
453+
Dfs2(i, j);
454+
}
455+
}
456+
457+
return ans;
458+
}
459+
}
460+
```
461+
402462
<!-- tabs:end -->
403463

404464
<!-- solution:end -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class Solution {
2+
public int MinimumScore(int[] nums, int[][] edges) {
3+
int n = nums.Length;
4+
List<int>[] g = new List<int>[n];
5+
for (int i = 0; i < n; i++) {
6+
g[i] = new List<int>();
7+
}
8+
foreach (var e in edges) {
9+
int a = e[0], b = e[1];
10+
g[a].Add(b);
11+
g[b].Add(a);
12+
}
13+
14+
int s = 0;
15+
foreach (int x in nums) {
16+
s ^= x;
17+
}
18+
19+
int ans = int.MaxValue;
20+
int s1 = 0;
21+
22+
int Dfs(int i, int fa) {
23+
int res = nums[i];
24+
foreach (int j in g[i]) {
25+
if (j != fa) {
26+
res ^= Dfs(j, i);
27+
}
28+
}
29+
return res;
30+
}
31+
32+
int Dfs2(int i, int fa) {
33+
int res = nums[i];
34+
foreach (int j in g[i]) {
35+
if (j != fa) {
36+
int s2 = Dfs2(j, i);
37+
res ^= s2;
38+
int mx = Math.Max(Math.Max(s ^ s1, s2), s1 ^ s2);
39+
int mn = Math.Min(Math.Min(s ^ s1, s2), s1 ^ s2);
40+
ans = Math.Min(ans, mx - mn);
41+
}
42+
}
43+
return res;
44+
}
45+
46+
for (int i = 0; i < n; ++i) {
47+
foreach (int j in g[i]) {
48+
s1 = Dfs(i, j);
49+
Dfs2(i, j);
50+
}
51+
}
52+
53+
return ans;
54+
}
55+
}

solution/3600-3699/3615.Longest Palindromic Path in Graph/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
-
88
- 字符串
99
- 动态规划
10+
- 状态压缩
1011
---
1112

1213
<!-- problem:start -->

solution/3600-3699/3615.Longest Palindromic Path in Graph/README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
- Graph
88
- String
99
- Dynamic Programming
10+
- Bitmask
1011
---
1112

1213
<!-- problem:start -->

solution/3600-3699/3616.Number of Student Replacements/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Number%20of%20Student%20Replacements/README.md
5+
tags:
6+
- 数组
7+
- 模拟
58
---
69

710
<!-- problem:start -->

solution/3600-3699/3616.Number of Student Replacements/README_EN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Number%20of%20Student%20Replacements/README_EN.md
5+
tags:
6+
- Array
7+
- Simulation
58
---
69

710
<!-- problem:start -->

0 commit comments

Comments
 (0)