Skip to content

Commit 732fbb6

Browse files
authored
Merge pull request #1186 from 0xff-dev/337
Add solution and test-cases for problem 337
2 parents 181f30e + 2edb438 commit 732fbb6

File tree

5 files changed

+78
-24
lines changed

5 files changed

+78
-24
lines changed
10.8 KB
Loading
12.8 KB
Loading

leetcode/301-400/0337.House-Robber-III/README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [337.House Robber III][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called `root`.
75

8-
**Example 1:**
6+
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**.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Given the `root` of the binary tree, return the maximum amount of money the thief can rob **without alerting the police**.
149

15-
## 题意
16-
> ...
10+
**Example 1:**
1711

18-
## 题解
12+
![1](./1.jpg)
1913

20-
### 思路1
21-
> ...
22-
House Robber III
23-
```go
14+
```
15+
Input: root = [3,2,3,null,3,null,1]
16+
Output: 7
17+
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
2418
```
2519

20+
**Example 2:**
21+
22+
![2](./2.jpg)
23+
```
24+
Input: root = [3,4,5,1,3,null,1]
25+
Output: 9
26+
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
27+
```
2628

2729
## 结语
2830

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left, Right *TreeNode
6+
}
7+
8+
func Solution(root *TreeNode) int {
9+
cache := make(map[*TreeNode][2]int)
10+
var dfs func(*TreeNode, bool) int
11+
dfs = func(tree *TreeNode, selectedParent bool) int {
12+
if tree == nil {
13+
return 0
14+
}
15+
if selectedParent {
16+
v, ok := cache[tree]
17+
if ok {
18+
if v[0] != -1 {
19+
return v[0]
20+
}
21+
}
22+
left := dfs(tree.Left, false)
23+
right := dfs(tree.Right, false)
24+
if !ok {
25+
v = [2]int{left + right, -1}
26+
} else {
27+
v[0] = left + right
28+
}
29+
cache[tree] = v
30+
return v[0]
31+
}
32+
v, ok := cache[tree]
33+
if !ok {
34+
v = [2]int{-1, -1}
35+
}
36+
if v[0] == -1 {
37+
left := dfs(tree.Left, false)
38+
right := dfs(tree.Right, false)
39+
v[0] = left + right
40+
}
41+
if v[1] == -1 {
42+
left1 := dfs(tree.Left, true)
43+
right1 := dfs(tree.Right, true)
44+
v[1] = left1 + right1 + tree.Val
45+
}
46+
cache[tree] = v
47+
return max(v[0], v[1])
48+
}
49+
return dfs(root, false)
550
}

leetcode/301-400/0337.House-Robber-III/Solution_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{
17+
Val: 3,
18+
Left: &TreeNode{Val: 2, Right: &TreeNode{Val: 3}},
19+
Right: &TreeNode{Val: 3, Right: &TreeNode{Val: 1}},
20+
}, 7},
21+
{"TestCase2", &TreeNode{
22+
Val: 3,
23+
Left: &TreeNode{Val: 4, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3}},
24+
Right: &TreeNode{Val: 5, Right: &TreeNode{Val: 1}},
25+
}, 9},
1926
}
2027

2128
// 开始测试
@@ -30,10 +37,10 @@ func TestSolution(t *testing.T) {
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}

0 commit comments

Comments
 (0)