Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7a4c5a

Browse files
feat: add solutions to lc problem: No.3068 (doocs#3418)
3068.Find the Maximum Sum of Node Values
1 parent 9f31a94 commit d7a4c5a

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

‎solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,28 @@ tags:
105105
#### Python3
106106

107107
```python
108-
108+
class Solution:
109+
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
110+
f0, f1 = 0, -inf
111+
for x in nums:
112+
f0, f1 = max(f0 + x, f1 + (x ^ k)), max(f1 + x, f0 + (x ^ k))
113+
return f0
109114
```
110115

111116
#### Java
112117

113118
```java
114-
119+
class Solution {
120+
public long maximumValueSum(int[] nums, int k, int[][] edges) {
121+
long f0 = 0, f1 = -0x3f3f3f3f;
122+
for (int x : nums) {
123+
long tmp = f0;
124+
f0 = Math.max(f0 + x, f1 + (x ^ k));
125+
f1 = Math.max(f1 + x, tmp + (x ^ k));
126+
}
127+
return f0;
128+
}
129+
}
115130
```
116131

117132
#### C++

‎solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,28 @@ It can be shown that 9 is the maximum achievable sum of values.
9797
#### Python3
9898

9999
```python
100-
100+
class Solution:
101+
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
102+
f0, f1 = 0, -inf
103+
for v in nums:
104+
f0, f1 = max(f0 + v, f1 + (v ^ k)), max(f1 + v, f0 + (v ^ k))
105+
return f0
101106
```
102107

103108
#### Java
104109

105110
```java
106-
111+
class Solution {
112+
public long maximumValueSum(int[] nums, int k, int[][] edges) {
113+
long f0 = 0, f1 = -0x3f3f3f3f;
114+
for (int x : nums) {
115+
long tmp = f0;
116+
f0 = Math.max(f0 + x, f1 + (x ^ k));
117+
f1 = Math.max(f1 + x, tmp + (x ^ k));
118+
}
119+
return f0;
120+
}
121+
}
107122
```
108123

109124
#### C++
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public long maximumValueSum(int[] nums, int k, int[][] edges) {
3+
long f0 = 0, f1 = -0x3f3f3f3f;
4+
for (int x : nums) {
5+
long tmp = f0;
6+
f0 = Math.max(f0 + x, f1 + (x ^ k));
7+
f1 = Math.max(f1 + x, tmp + (x ^ k));
8+
}
9+
return f0;
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
3+
f0, f1 = 0, -inf
4+
for x in nums:
5+
f0, f1 = max(f0 + x, f1 + (x ^ k)), max(f1 + x, f0 + (x ^ k))
6+
return f0
29.2 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /