Skip to content

Commit ab0b9b4

Browse files
committed
Time: 924 ms (97.73%), Space: 28.1 MB (50.57%) - LeetHub
1 parent 3eecc99 commit ab0b9b4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# time complexity: O(n)
2+
# space complexity: O(1)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
8+
sumVal = 0
9+
count = 0
10+
positiveMinimum = 1 << 30
11+
negativeMaximum = -1 * (1 << 30)
12+
13+
for nodeValue in nums:
14+
operatedNodeValue = nodeValue ^ k
15+
sumVal += nodeValue
16+
netChange = operatedNodeValue - nodeValue
17+
if netChange > 0:
18+
positiveMinimum = min(positiveMinimum, netChange)
19+
sumVal += netChange
20+
count += 1
21+
else:
22+
negativeMaximum = max(negativeMaximum, netChange)
23+
24+
if count % 2 == 0:
25+
return sumVal
26+
27+
return max(sumVal - positiveMinimum, sumVal + negativeMaximum)
28+
29+
30+
nums = [7, 7, 7, 7, 7, 7]
31+
k = 3
32+
edges = [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5]]
33+
34+
print(Solution().maximumValueSum(nums, k, edges))

0 commit comments

Comments
 (0)