Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6850a23

Browse files
author
侯利朋
committed
.\337.house-robber-iii ok
1 parent 4e0fb19 commit 6850a23

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

337.house-robber-iii-680ms.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=337 lang=golang
3+
*
4+
* [337] House Robber III
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// (680 ms) √ Your runtime beats 27.08 %
15+
// 递归, 非DP实现
16+
func rob(root *TreeNode) int {
17+
if root == nil {
18+
return 0
19+
}
20+
21+
// 打劫root
22+
val := root.Val
23+
if root.Left != nil {
24+
val += rob(root.Left.Left) + rob(root.Left.Right)
25+
}
26+
if root.Right != nil {
27+
val += rob(root.Right.Left) + rob(root.Right.Right)
28+
}
29+
return max(val, rob(root.Left)+rob(root.Right)) // 返回打劫root和不打劫root的最大值
30+
}
31+
32+
func max(a, b int) int {
33+
if a > b {
34+
return a
35+
}
36+
return b
37+
}

337.house-robber-iii-8ms.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @lc app=leetcode id=337 lang=golang
3+
*
4+
* [337] House Robber III
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// 8 ms, faster than 72.92%
15+
// 递归, 非DP实现
16+
// map 缓存加速
17+
func rob(root *TreeNode) int {
18+
if root == nil {
19+
return 0
20+
}
21+
buf := make(map[*TreeNode]int)
22+
return robmap(root, buf)
23+
}
24+
25+
func robmap(root *TreeNode, buf map[*TreeNode]int) int {
26+
if root == nil {
27+
return 0
28+
}
29+
if buf[root] > 0 {
30+
return buf[root]
31+
}
32+
// 打劫root
33+
val := root.Val
34+
if root.Left != nil {
35+
val += robmap(root.Left.Left, buf) + robmap(root.Left.Right, buf)
36+
}
37+
if root.Right != nil {
38+
val += robmap(root.Right.Left, buf) + robmap(root.Right.Right, buf)
39+
}
40+
41+
rtn := max(val, robmap(root.Left, buf)+robmap(root.Right, buf))
42+
buf[root] = rtn
43+
return rtn // 返回打劫root和不打劫root的最大值
44+
}
45+
46+
func max(a, b int) int {
47+
if a > b {
48+
return a
49+
}
50+
return b
51+
}

337.house-robber-iii.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=337 lang=golang
3+
*
4+
* [337] House Robber III
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// 待优化,吧下面的map去掉,找到dp之间的关系,就能进一步节省空间
15+
func rob(root *TreeNode) int {
16+
if root == nil {
17+
return 0
18+
}
19+
buf := make(map[*TreeNode]int)
20+
return robmap(root, buf)
21+
}
22+
23+
func robmap(root *TreeNode, buf map[*TreeNode]int) int {
24+
if root == nil {
25+
return 0
26+
}
27+
if buf[root] > 0 {
28+
return buf[root]
29+
}
30+
// 打劫root
31+
val := root.Val
32+
if root.Left != nil {
33+
val += robmap(root.Left.Left, buf) + robmap(root.Left.Right, buf)
34+
}
35+
if root.Right != nil {
36+
val += robmap(root.Right.Left, buf) + robmap(root.Right.Right, buf)
37+
}
38+
39+
rtn := max(val, robmap(root.Left, buf)+robmap(root.Right, buf))
40+
buf[root] = rtn
41+
return rtn // 返回打劫root和不打劫root的最大值
42+
}
43+
44+
func max(a, b int) int {
45+
if a > b {
46+
return a
47+
}
48+
return b
49+
}

0 commit comments

Comments
 (0)