-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path337.打家劫舍-iii.js
72 lines (60 loc) · 1.75 KB
/
337.打家劫舍-iii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @lc app=leetcode.cn id=337 lang=javascript
*
* [337] 打家劫舍 III
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var rob = function(root) {
/**
* 该种方案会超时
*/
// const memo = new Map();
// function doRob (node) {
// if (node === null) return 0;
// // 利用备忘录消除重叠子问题
// if (memo.has(memo)) return memo.get(node);
// // 抢,然后去下下家
// const do_it = node.val
// + (node.left === null ? 0 : doRob(node.left.left) + doRob(node.left.right))
// + (node.right === null ? 0 : doRob(node.right.left) + doRob(node.right.right));
// // 不抢,然后去下家
// const not_do_it = doRob(node.left) + doRob(node.right);
// const res = Math.max(do_it, not_do_it);
// memo.set(node, res);
// return res;
// }
// return doRob(root);
/**
* 不会超时
*/
const res = dp(root);
/*
返回一个大小为 2 的数组 arr
arr[0] 表示不抢 root 的话,得到的最大钱数
arr[1] 表示抢 root 的话,得到的最大钱数
*/
function dp(root) {
if (root === null) return [0, 0];
const left = dp(root.left);
const right = dp(root.right);
// 抢,下家就不能抢了
const rob = root.val + left[0] + right[0];
// 不抢,下家可抢可不抢,取决于收益大小
const not_rob = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return [not_rob, rob]
}
return Math.max(res[0], res[1])
};
// @lc code=end