Skip to content

Commit 51b984f

Browse files
authored
feat: add cpp solution to lc problem: No.3068 (#2839)
1 parent 3f6a34f commit 51b984f

File tree

1 file changed

+29
-0
lines changed
  • solution/3000-3099/3068.Find the Maximum Sum of Node Values

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
long long maximumValueSum(vector<int>& nums, int k,
4+
vector<vector<int>>& edges) {
5+
long long totalSum = 0;
6+
int count = 0;
7+
int positiveMin = INT_MAX;
8+
int negativeMax = INT_MIN;
9+
10+
for (int nodeValue : nums) {
11+
int nodeValAfterOperation = nodeValue ^ k;
12+
totalSum += nodeValue;
13+
int netChange = nodeValAfterOperation - nodeValue;
14+
15+
if (netChange > 0) {
16+
positiveMin = min(positiveMin, netChange);
17+
totalSum += netChange;
18+
count += 1;
19+
} else {
20+
negativeMax = max(negativeMax, netChange);
21+
}
22+
}
23+
24+
if (count % 2 == 0) {
25+
return totalSum;
26+
}
27+
return max(totalSum - positiveMin, totalSum + negativeMax);
28+
}
29+
};

0 commit comments

Comments
 (0)