diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md index 83e99e58f23d9..b527f4b4f3be4 100644 --- a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README.md @@ -105,13 +105,28 @@ tags: #### Python3 ```python - +class Solution: + def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int: + f0, f1 = 0, -inf + for x in nums: + f0, f1 = max(f0 + x, f1 + (x ^ k)), max(f1 + x, f0 + (x ^ k)) + return f0 ``` #### Java ```java - +class Solution { + public long maximumValueSum(int[] nums, int k, int[][] edges) { + long f0 = 0, f1 = -0x3f3f3f3f; + for (int x : nums) { + long tmp = f0; + f0 = Math.max(f0 + x, f1 + (x ^ k)); + f1 = Math.max(f1 + x, tmp + (x ^ k)); + } + return f0; + } +} ``` #### C++ diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md index 02f7391356a66..de45a57904708 100644 --- a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md @@ -97,13 +97,28 @@ It can be shown that 9 is the maximum achievable sum of values. #### Python3 ```python - +class Solution: + def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int: + f0, f1 = 0, -inf + for v in nums: + f0, f1 = max(f0 + v, f1 + (v ^ k)), max(f1 + v, f0 + (v ^ k)) + return f0 ``` #### Java ```java - +class Solution { + public long maximumValueSum(int[] nums, int k, int[][] edges) { + long f0 = 0, f1 = -0x3f3f3f3f; + for (int x : nums) { + long tmp = f0; + f0 = Math.max(f0 + x, f1 + (x ^ k)); + f1 = Math.max(f1 + x, tmp + (x ^ k)); + } + return f0; + } +} ``` #### C++ diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.java b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.java new file mode 100644 index 0000000000000..a5dbc45c9e130 --- /dev/null +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public long maximumValueSum(int[] nums, int k, int[][] edges) { + long f0 = 0, f1 = -0x3f3f3f3f; + for (int x : nums) { + long tmp = f0; + f0 = Math.max(f0 + x, f1 + (x ^ k)); + f1 = Math.max(f1 + x, tmp + (x ^ k)); + } + return f0; + } +} diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.py b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.py new file mode 100644 index 0000000000000..0e244763277cb --- /dev/null +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.py @@ -0,0 +1,6 @@ +class Solution: + def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int: + f0, f1 = 0, -inf + for x in nums: + f0, f1 = max(f0 + x, f1 + (x ^ k)), max(f1 + x, f0 + (x ^ k)) + return f0 diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/images/screenshot-2023-11-10-012641.png b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/images/screenshot-2023-11-10-012641.png new file mode 100644 index 0000000000000..4caff07b2cec6 Binary files /dev/null and b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/images/screenshot-2023-11-10-012641.png differ