From a1c027b413c32ef988e48a5abbae5672e3abaf57 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 24 Jul 2025 07:07:51 +0800 Subject: [PATCH] feat: add solution to lc problem: No.2322 No.2322.Minimum Score After Removals on a Tree --- .../README.md | 4 +- .../README_EN.md | 20 +++---- .../README.md | 60 +++++++++++++++++++ .../README_EN.md | 60 +++++++++++++++++++ .../Solution.cs | 55 +++++++++++++++++ 5 files changed, 187 insertions(+), 12 deletions(-) create mode 100644 solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cs diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md index ee4d646a0ed9a..188d7f4e4bd69 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md @@ -88,9 +88,9 @@ tags: - 如果 $c$ 是 "a",由于要先删除 "ab",因此此时我们不消除该字符,只增加 $\textit{cnt1}$; - 如果 $c$ 是 "b",如果此时 $\textit{cnt1} > 0$,我们可以消除一个 "ab",并增加 $x$ 分,否则我们只能增加 $\textit{cnt2}$; -- 如果 $c$ 是其他字符,那么对于该子字符串,我们剩下了一个 $\textit{cnt2}$ 个 "b" 和 $\textit{cnt1}$ 个 "a",我们可以消除 $\min(\textit{cnt1}, \textit{cnt2})$ 个 "ab",并增加 $y$ 分。 +- 如果 $c$ 是其他字符,那么对于该子字符串,我们剩下了 $\textit{cnt2}$ 个 "b" 和 $\textit{cnt1}$ 个 "a",我们可以消除 $\min(\textit{cnt1}, \textit{cnt2})$ 个 "ba",并增加若干个 $y$ 分。 -遍历结束后,我们还需要额外处理一下剩余的 "ab",增加若干个 $y$ 分。 +遍历结束后,我们还需要额外处理一下剩余的 "ba",增加若干个 $y$ 分。 时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md index 5bd2436a0f9ed..bad524814c1b2 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md @@ -76,23 +76,23 @@ Total score = 5 + 4 + 5 + 5 = 19. ### Solution 1: Greedy -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$. +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$. -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. +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. -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. +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. -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. +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. -For the current character $c$: +For the current character $c$ being traversed: -- If $c$ is "a", since we need to delete "ab" first, we do not eliminate this character at this time, only increase $\textit{cnt1}$; -- 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}$; -- 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. +- If $c$ is "a", since we want to remove "ab" first, we don't eliminate this character at this time, only increment $\textit{cnt1}$; +- 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}$; +- 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. -After the traversal is finished, we also need to additionally handle the remaining "ab", adding several $y$ points. +After traversal, we need to handle the remaining "ba"s and add several $y$ points. -The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of string $s$. The space complexity is $O(1)$. diff --git a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README.md b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README.md index 83c4fe4f157fc..65904785b7ae2 100644 --- a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README.md +++ b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README.md @@ -399,6 +399,66 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int MinimumScore(int[] nums, int[][] edges) { + int n = nums.Length; + List[] g = new List[n]; + for (int i = 0; i < n; i++) { + g[i] = new List(); + } + foreach (var e in edges) { + int a = e[0], b = e[1]; + g[a].Add(b); + g[b].Add(a); + } + + int s = 0; + foreach (int x in nums) { + s ^= x; + } + + int ans = int.MaxValue; + int s1 = 0; + + int Dfs(int i, int fa) { + int res = nums[i]; + foreach (int j in g[i]) { + if (j != fa) { + res ^= Dfs(j, i); + } + } + return res; + } + + int Dfs2(int i, int fa) { + int res = nums[i]; + foreach (int j in g[i]) { + if (j != fa) { + int s2 = Dfs2(j, i); + res ^= s2; + int mx = Math.Max(Math.Max(s ^ s1, s2), s1 ^ s2); + int mn = Math.Min(Math.Min(s ^ s1, s2), s1 ^ s2); + ans = Math.Min(ans, mx - mn); + } + } + return res; + } + + for (int i = 0; i < n; ++i) { + foreach (int j in g[i]) { + s1 = Dfs(i, j); + Dfs2(i, j); + } + } + + return ans; + } +} +``` + diff --git a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README_EN.md b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README_EN.md index 7019099c43e42..a2e646213d544 100644 --- a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README_EN.md +++ b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/README_EN.md @@ -399,6 +399,66 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int MinimumScore(int[] nums, int[][] edges) { + int n = nums.Length; + List[] g = new List[n]; + for (int i = 0; i < n; i++) { + g[i] = new List(); + } + foreach (var e in edges) { + int a = e[0], b = e[1]; + g[a].Add(b); + g[b].Add(a); + } + + int s = 0; + foreach (int x in nums) { + s ^= x; + } + + int ans = int.MaxValue; + int s1 = 0; + + int Dfs(int i, int fa) { + int res = nums[i]; + foreach (int j in g[i]) { + if (j != fa) { + res ^= Dfs(j, i); + } + } + return res; + } + + int Dfs2(int i, int fa) { + int res = nums[i]; + foreach (int j in g[i]) { + if (j != fa) { + int s2 = Dfs2(j, i); + res ^= s2; + int mx = Math.Max(Math.Max(s ^ s1, s2), s1 ^ s2); + int mn = Math.Min(Math.Min(s ^ s1, s2), s1 ^ s2); + ans = Math.Min(ans, mx - mn); + } + } + return res; + } + + for (int i = 0; i < n; ++i) { + foreach (int j in g[i]) { + s1 = Dfs(i, j); + Dfs2(i, j); + } + } + + return ans; + } +} +``` + diff --git a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cs b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cs new file mode 100644 index 0000000000000..392ae45d1e65d --- /dev/null +++ b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cs @@ -0,0 +1,55 @@ +public class Solution { + public int MinimumScore(int[] nums, int[][] edges) { + int n = nums.Length; + List[] g = new List[n]; + for (int i = 0; i < n; i++) { + g[i] = new List(); + } + foreach (var e in edges) { + int a = e[0], b = e[1]; + g[a].Add(b); + g[b].Add(a); + } + + int s = 0; + foreach (int x in nums) { + s ^= x; + } + + int ans = int.MaxValue; + int s1 = 0; + + int Dfs(int i, int fa) { + int res = nums[i]; + foreach (int j in g[i]) { + if (j != fa) { + res ^= Dfs(j, i); + } + } + return res; + } + + int Dfs2(int i, int fa) { + int res = nums[i]; + foreach (int j in g[i]) { + if (j != fa) { + int s2 = Dfs2(j, i); + res ^= s2; + int mx = Math.Max(Math.Max(s ^ s1, s2), s1 ^ s2); + int mn = Math.Min(Math.Min(s ^ s1, s2), s1 ^ s2); + ans = Math.Min(ans, mx - mn); + } + } + return res; + } + + for (int i = 0; i < n; ++i) { + foreach (int j in g[i]) { + s1 = Dfs(i, j); + Dfs2(i, j); + } + } + + return ans; + } +}