Skip to content
LC-3068 Hard LeetCode

3068. Find the Maximum Sum of Node Values

Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 65% Topics: Array, Dynamic Programming, Greedy, Bit Manipulation, Tree, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# greedy
class Solution(object):
    def maximumValueSum(self, nums, k, edges):
        """
        :type nums: List[int]
        :type k: int
        :type edges: List[List[int]]
        :rtype: int
        """
        result = parity = 0
        diff = float("inf")
        for x in nums:
            y = x^k
            result += max(x, y)
            parity ^= int(x < y)
            diff = min(diff, abs(x-y))
        return result-parity*diff

Solution from kamyu104/LeetCode-Solutions · MIT