diff --git a/leetcode/301-400/0337.House-Robber-III/1.jpg b/leetcode/301-400/0337.House-Robber-III/1.jpg new file mode 100644 index 000000000..1e4081a11 Binary files /dev/null and b/leetcode/301-400/0337.House-Robber-III/1.jpg differ diff --git a/leetcode/301-400/0337.House-Robber-III/2.jpg b/leetcode/301-400/0337.House-Robber-III/2.jpg new file mode 100644 index 000000000..1b6b17a16 Binary files /dev/null and b/leetcode/301-400/0337.House-Robber-III/2.jpg differ diff --git a/leetcode/301-400/0337.House-Robber-III/README.md b/leetcode/301-400/0337.House-Robber-III/README.md index 3c918ffdc..397634a52 100644 --- a/leetcode/301-400/0337.House-Robber-III/README.md +++ b/leetcode/301-400/0337.House-Robber-III/README.md @@ -1,28 +1,30 @@ # [337.House Robber III][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 +The thief has found himself a new place for his thievery again. There is only one entrance to this area, called `root`. -**Example 1:** +Besides the `root`, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if **two directly-linked houses were broken into on the same night**. -``` -Input: a = "11", b = "1" -Output: "100" -``` +Given the `root` of the binary tree, return the maximum amount of money the thief can rob **without alerting the police**. -## 题意 -> ... +**Example 1:** -## 题解 +![1](./1.jpg) -### 思路1 -> ... -House Robber III -```go +``` +Input: root = [3,2,3,null,3,null,1] +Output: 7 +Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. ``` +**Example 2:** + +![2](./2.jpg) +``` +Input: root = [3,4,5,1,3,null,1] +Output: 9 +Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9. +``` ## 结语 diff --git a/leetcode/301-400/0337.House-Robber-III/Solution.go b/leetcode/301-400/0337.House-Robber-III/Solution.go index d115ccf5e..dea53ffac 100644 --- a/leetcode/301-400/0337.House-Robber-III/Solution.go +++ b/leetcode/301-400/0337.House-Robber-III/Solution.go @@ -1,5 +1,50 @@ package Solution -func Solution(x bool) bool { - return x +type TreeNode struct { + Val int + Left, Right *TreeNode +} + +func Solution(root *TreeNode) int { + cache := make(map[*TreeNode][2]int) + var dfs func(*TreeNode, bool) int + dfs = func(tree *TreeNode, selectedParent bool) int { + if tree == nil { + return 0 + } + if selectedParent { + v, ok := cache[tree] + if ok { + if v[0] != -1 { + return v[0] + } + } + left := dfs(tree.Left, false) + right := dfs(tree.Right, false) + if !ok { + v = [2]int{left + right, -1} + } else { + v[0] = left + right + } + cache[tree] = v + return v[0] + } + v, ok := cache[tree] + if !ok { + v = [2]int{-1, -1} + } + if v[0] == -1 { + left := dfs(tree.Left, false) + right := dfs(tree.Right, false) + v[0] = left + right + } + if v[1] == -1 { + left1 := dfs(tree.Left, true) + right1 := dfs(tree.Right, true) + v[1] = left1 + right1 + tree.Val + } + cache[tree] = v + return max(v[0], v[1]) + } + return dfs(root, false) } diff --git a/leetcode/301-400/0337.House-Robber-III/Solution_test.go b/leetcode/301-400/0337.House-Robber-III/Solution_test.go index 14ff50eb4..2f466c2c2 100644 --- a/leetcode/301-400/0337.House-Robber-III/Solution_test.go +++ b/leetcode/301-400/0337.House-Robber-III/Solution_test.go @@ -10,12 +10,19 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs *TreeNode + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", &TreeNode{ + Val: 3, + Left: &TreeNode{Val: 2, Right: &TreeNode{Val: 3}}, + Right: &TreeNode{Val: 3, Right: &TreeNode{Val: 1}}, + }, 7}, + {"TestCase2", &TreeNode{ + Val: 3, + Left: &TreeNode{Val: 4, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3}}, + Right: &TreeNode{Val: 5, Right: &TreeNode{Val: 1}}, + }, 9}, } // 开始测试 @@ -30,10 +37,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }