-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFindTheMaximumSumOfNodeValues.cpp
More file actions
45 lines (36 loc) · 1.28 KB
/
Copy pathFindTheMaximumSumOfNodeValues.cpp
File metadata and controls
45 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
// When would XOR increase a value?
// What if it increases one but decreases other?
// One num can be XOR'd multiple times
// Does a traversal make sense?
// XOR is not a blackbox, n ^ k ^ k = n
// Thus we can (and must) XOR any two nodes at a time
// Greedy, sort by XOR delta
long long result = 0;
vector<long long> delta;
for(long long i=0; i<nums.size(); i++) {
result += nums[i];
delta.push_back((nums[i] ^ k) - nums[i]);
}
sort(delta.begin(), delta.end(), greater<>());
for(long long i=0; i<delta.size(); i+= 2) {
if(i == delta.size() - 1) {
break;
}
long long path_delta = delta[i] + delta[i + 1];
if(path_delta <= 0) {
break;
}
result += path_delta;
}
return result;
}
};
// Tree is a connected graph that doesn't have cycles in it
// n ^ k ^ k = n
// either you leave the node as it is or XOR nodes twice
// maximize the total sum of values
// Time Complexity - O(NlogN)
// Space Complexity - O(N), extra space