Skip to content

Commit

Permalink
Merge pull request #901 from 0xff-dev/3068
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 3068
  • Loading branch information
6boris authored Jun 14, 2024
2 parents ba52aa8 + 60e3ca1 commit 206dc0b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
# [3068.Find the Maximum Sum of Node Values][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a **0-indexed** 2D integer array `edges` of length `n - 1`, where `edges[i] = [ui, vi]` indicates that there is an edge between nodes u<sub>i</sub> and v<sub>i</sub> in the tree. You are also given a **positive** integer `k`, and a **0-indexed** array of **non-negative** integers `nums` of length `n`, where `nums[i]` represents the **value** of the node numbered `i`.

Alice wants the sum of values of tree nodes to be **maximum**, for which Alice can perform the following operation **any** number of times (**including zero**) on the tree:

- Choose any edge `[u, v]` connecting the nodes `u` and `v`, and update their values as follows:

- `nums[u] = nums[u] XOR k`
- `nums[v] = nums[v] XOR k`

Return the **maximum** possible **sum** of the **values** Alice can achieve by performing the operation **any** number of times.

**Example 1:**

**Example 1:**
![1](./screenshot-2023-11-10-012513.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
Output: 6
Explanation: Alice can achieve the maximum sum of 6 using a single operation:
- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
The total sum of values is 2 + 2 + 2 = 6.
It can be shown that 6 is the maximum achievable sum of values.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./screenshot-2024-01-09-220017.png)

### 思路1
> ...
Find the Maximum Sum of Node Values
```go
```
Input: nums = [2,3], k = 7, edges = [[0,1]]
Output: 9
Explanation: Alice can achieve the maximum sum of 9 using a single operation:
- 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] -> [5,4].
The total sum of values is 5 + 4 = 9.
It can be shown that 9 is the maximum achievable sum of values.
```

**Example 3:**

![3](./screenshot-2023-11-10-012641.png)

```
Input: nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
Output: 42
Explanation: The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(nums []int, k int, edges [][]int) int64 {
// 经过测试,[0,1],[1,2] [1,2,1]这组数据也是6,就是说他选的任何节点俩节点,都可以
// 那么我们关注的就是,最大值从小大排列, 而且题目保证了是一棵树,
// 所以,只需要每次选俩和是正的就ok

diff := make([]int, len(nums))
ans := int64(0)
for i, v := range nums {
ans += int64(v)
diff[i] = v ^ k - v
}

sort.Slice(diff, func(i, j int) bool {
return diff[i] > diff[j]
})
for i := 0; i < len(diff); i += 2 {
if i+1 == len(diff) {
break
}
if s := int64(diff[i]) + int64(diff[i+1]); s > 0 {
ans += s
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
edges [][]int
k int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 1}, [][]int{{0, 1}, {0, 2}}, 3, 6},
{"TestCase2", []int{2, 3}, [][]int{{0, 1}}, 7, 9},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.k, c.edges)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.nums, c.edges, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 206dc0b

Please sign in to comment.