We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3f6a34f commit 51b984fCopy full SHA for 51b984f
solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp
@@ -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