Skip to content

feat: add solution to lc problem: No.2322 #4592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ Total score = 5 + 4 + 5 + 5 = 19.</pre>

### 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)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,66 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MinimumScore(int[] nums, int[][] edges) {
int n = nums.Length;
List<int>[] g = new List<int>[n];
for (int i = 0; i < n; i++) {
g[i] = new List<int>();
}
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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,66 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MinimumScore(int[] nums, int[][] edges) {
int n = nums.Length;
List<int>[] g = new List<int>[n];
for (int i = 0; i < n; i++) {
g[i] = new List<int>();
}
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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class Solution {
public int MinimumScore(int[] nums, int[][] edges) {
int n = nums.Length;
List<int>[] g = new List<int>[n];
for (int i = 0; i < n; i++) {
g[i] = new List<int>();
}
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;
}
}