Skip to content

Commit ac3102d

Browse files
authored
feat: add cpp solution to lc problem: No.3068 (doocs#2841)
1 parent f55b0d7 commit ac3102d

File tree

1 file changed

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

1 file changed

+29
-1
lines changed

solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,35 @@ tags:
117117
#### C++
118118

119119
```cpp
120-
120+
class Solution {
121+
public:
122+
long long maximumValueSum(vector<int>& nums, int k,
123+
vector<vector<int>>& edges) {
124+
long long totalSum = 0;
125+
int count = 0;
126+
int positiveMin = INT_MAX;
127+
int negativeMax = INT_MIN;
128+
129+
for (int nodeValue : nums) {
130+
int nodeValAfterOperation = nodeValue ^ k;
131+
totalSum += nodeValue;
132+
int netChange = nodeValAfterOperation - nodeValue;
133+
134+
if (netChange > 0) {
135+
positiveMin = min(positiveMin, netChange);
136+
totalSum += netChange;
137+
count += 1;
138+
} else {
139+
negativeMax = max(negativeMax, netChange);
140+
}
141+
}
142+
143+
if (count % 2 == 0) {
144+
return totalSum;
145+
}
146+
return max(totalSum - positiveMin, totalSum + negativeMax);
147+
}
148+
};
121149
```
122150

123151
#### Go

0 commit comments

Comments
 (0)