From 5ef0e4444e14887b819e0d373a9a7d6f2f33d4e8 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 10 Dec 2022 11:04:17 +0330 Subject: 1339 --- 1339/go.mod | 3 +++ 1339/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 1339/go.mod create mode 100644 1339/main.go diff --git a/1339/go.mod b/1339/go.mod new file mode 100644 index 0000000..aa2e740 --- /dev/null +++ b/1339/go.mod @@ -0,0 +1,3 @@ +module 1339 + +go 1.19 diff --git a/1339/main.go b/1339/main.go new file mode 100644 index 0000000..8eed077 --- /dev/null +++ b/1339/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" +) + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func max(a, b int64) int64 { + if a > b { + return int64(a) + } else { + return int64(b) + } +} + +func maxProduct(root *TreeNode) int { + var total int64 + var result int64 + + var sumUnder func(*TreeNode) int64 + sumUnder = func(node *TreeNode) int64 { + var sum int64 + if node == nil { + return 0 + } + + sum = sumUnder(node.Left) + sumUnder(node.Right) + int64(node.Val) + result = max(int64(result), sum*(total-sum)) + + return sum + } + + total = sumUnder(root) + sumUnder(root) + + return int(result % (1_000_000_000 + 7)) +} + +func main() { + fmt.Println("vim-go") +} -- cgit v1.2.3