java: add «3068. Find the Maximum Sum of Node Values»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
3028c01721
commit
1ef3050db6
1 changed files with 22 additions and 0 deletions
22
java/find-the-maximum-sum-of-node-values.java
Normal file
22
java/find-the-maximum-sum-of-node-values.java
Normal 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];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue