Skip to content

Add solution and test-cases for problem 337 #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/301-400/0337.House-Robber-III/1.jpg
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 leetcode/301-400/0337.House-Robber-III/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 17 additions & 15 deletions leetcode/301-400/0337.House-Robber-III/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
49 changes: 47 additions & 2 deletions leetcode/301-400/0337.House-Robber-III/Solution.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 14 additions & 7 deletions leetcode/301-400/0337.House-Robber-III/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +37,10 @@ func TestSolution(t *testing.T) {
}
}

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading