-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0352.py
22 lines (19 loc) · 956 Bytes
/
LC0352.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def maxSumOfNodes(self, index, isEven, nums, k, memo):
if index == len(nums):
# If the operation is performed on an odd number of elements return INT_MIN
return 0 if isEven == 1 else -float("inf")
if memo[index][isEven] != -1:
return memo[index][isEven]
# No operation performed on the element
noXorDone = nums[index] + self.maxSumOfNodes(index + 1, isEven, nums, k, memo)
# XOR operation is performed on the element
xorDone = (nums[index] ^ k) + self.maxSumOfNodes(
index + 1, isEven ^ 1, nums, k, memo
)
# Memoize and return the result
memo[index][isEven] = max(xorDone, noXorDone)
return memo[index][isEven]
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
memo = [[-1] * 2 for _ in range(len(nums))]
return self.maxSumOfNodes(0, 1, nums, k, memo)