Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f55b0d7

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

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_EN.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,35 @@ It can be shown that 9 is the maximum achievable sum of values.
109109
#### C++
110110

111111
```cpp
112-
112+
class Solution {
113+
public:
114+
long long maximumValueSum(vector<int>& nums, int k,
115+
vector<vector<int>>& edges) {
116+
long long totalSum = 0;
117+
int count = 0;
118+
int positiveMin = INT_MAX;
119+
int negativeMax = INT_MIN;
120+
121+
for (int nodeValue : nums) {
122+
int nodeValAfterOperation = nodeValue ^ k;
123+
totalSum += nodeValue;
124+
int netChange = nodeValAfterOperation - nodeValue;
125+
126+
if (netChange > 0) {
127+
positiveMin = min(positiveMin, netChange);
128+
totalSum += netChange;
129+
count += 1;
130+
} else {
131+
negativeMax = max(negativeMax, netChange);
132+
}
133+
}
134+
135+
if (count % 2 == 0) {
136+
return totalSum;
137+
}
138+
return max(totalSum - positiveMin, totalSum + negativeMax);
139+
}
140+
};
113141
```
114142

115143
#### Go

0 commit comments

Comments
 (0)