-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #901 from 0xff-dev/3068
Add solution and test-cases for problem 3068
- Loading branch information
Showing
6 changed files
with
74 additions
and
25 deletions.
There are no files selected for viewing
50 changes: 37 additions & 13 deletions
50
leetcode/3001-3100/3068.Find-the-Maximum-Sum-of-Node-Values/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 26 additions & 2 deletions
28
leetcode/3001-3100/3068.Find-the-Maximum-Sum-of-Node-Values/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+20.5 KB
...-3100/3068.Find-the-Maximum-Sum-of-Node-Values/screenshot-2023-11-10-012513.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.2 KB
...-3100/3068.Find-the-Maximum-Sum-of-Node-Values/screenshot-2023-11-10-012641.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.1 KB
...-3100/3068.Find-the-Maximum-Sum-of-Node-Values/screenshot-2024-01-09-220017.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.