Skip to content

Commit ef57406

Browse files
committed
[LeetCode Sync] Runtime - 36 ms (71.93%), Memory - 25.9 MB (91.23%)
1 parent 53cd884 commit ef57406

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p>
2+
3+
<p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p>
4+
5+
<ul>
6+
<li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows:
7+
8+
<ul>
9+
<li><code>nums[u] = nums[u] XOR k</code></li>
10+
<li><code>nums[v] = nums[v] XOR k</code></li>
11+
</ul>
12+
</li>
13+
</ul>
14+
15+
<p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" />
20+
<pre>
21+
<strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
22+
<strong>Output:</strong> 6
23+
<strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation:
24+
- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2].
25+
The total sum of values is 2 + 2 + 2 = 6.
26+
It can be shown that 6 is the maximum achievable sum of values.
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" />
31+
<pre>
32+
<strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]]
33+
<strong>Output:</strong> 9
34+
<strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation:
35+
- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4].
36+
The total sum of values is 5 + 4 = 9.
37+
It can be shown that 9 is the maximum achievable sum of values.
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" />
42+
<pre>
43+
<strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
44+
<strong>Output:</strong> 42
45+
<strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li>
53+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
54+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
55+
<li><code>edges.length == n - 1</code></li>
56+
<li><code>edges[i].length == 2</code></li>
57+
<li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li>
58+
<li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li>
59+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
3+
sumVal = 0
4+
count = 0
5+
positiveMinimum = 1 << 30
6+
negativeMaximum = -1 * (1 << 30)
7+
8+
for nodeValue in nums:
9+
operatedNodeValue = nodeValue ^ k
10+
sumVal += nodeValue
11+
netChange = operatedNodeValue - nodeValue
12+
if netChange > 0:
13+
positiveMinimum = min(positiveMinimum, netChange)
14+
sumVal += netChange
15+
count += 1
16+
else:
17+
negativeMaximum = max(negativeMaximum, netChange)
18+
19+
# If the number of positive netChange values is even, return the sum.
20+
if count % 2 == 0:
21+
return sumVal
22+
23+
# Otherwise return the maximum of both discussed cases.
24+
return max(sumVal - positiveMinimum, sumVal + negativeMaximum)

0 commit comments

Comments
 (0)