1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00

java: add «3068. Find the Maximum Sum of Node Values»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-05-20 14:55:08 +02:00
parent 3028c01721
commit 1ef3050db6
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,22 @@
class Solution {
private static final int[] EVEN = new int[] {0, 1};
public long maximumValueSum(int[] nums, int k, int[][] edges) {
int n = nums.length;
long[][] dp = new long[n + 1][2];
dp[n][0] = Integer.MIN_VALUE;
dp[n][1] = 0;
for (int i = n - 1; i >= 0; --i) {
for (int even : EVEN) {
var xor = dp[i + 1][even ^ 1] + (nums[i] ^ k);
var skip = dp[i + 1][even] + nums[i];
dp[i][even] = Math.max(xor, skip);
}
}
return dp[0][1];
}
}