Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 56f9879

Browse files
find-the-maximum-sum-of-node-values (#5)
Create find-the-maximum-sum-of-node-values.cs
2 parents 6b8ebbc + b948423 commit 56f9879

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** Time: O(n) **/
2+
3+
public class Solution {
4+
public long MaximumValueSum(int[] nums, int k, int[][] edges)
5+
{
6+
long result = 0;
7+
int diff = int.MaxValue;
8+
int parity = 0;
9+
10+
foreach (var x in nums) {
11+
var y = x ^ k;
12+
result += Math.Max(x, y);
13+
parity ^= x < y ? 1 : 0;
14+
diff = Math.Min(diff, Math.Abs(x - y));
15+
}
16+
17+
return result - parity * diff;
18+
}
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /