337-House Robber III
Description
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” 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 forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
問題描述
小偷發現來偷東西的新地點。這塊區域只有一個入口, 被稱作”root”.除了root之外, 每個房間有且僅有一個父房間。轉了一圈之後, 小猴意識到這塊區域的所有房間形成了一個二叉樹。如果兩個直接連線的房間在同一晚被偷, 那麼警報會被觸發。
在不會觸發警報的情況下, 找出小偷當天可以獲得的最大的金額。
問題分析
強烈建議看一下這個連結
https://leetcode.com/problems/house-robber-iii/discuss/79330/Step-by-step-tackling-of-the-problem
解法1
class Solution {
public int rob(TreeNode root) {
if (root == null) return 0;
int val = 0;
if (root.left != null) val += rob(root.left.left) + rob(root.left.right);
if (root.right != null) val += rob(root.right.left) + rob(root.right.right);
//兩種情況, 當前節點偷與不偷
return Math.max(val + root.val, rob(root.left) + rob(root.right));
}
}
解法2
/*
與解法1的不同之處在於使用map儲存了中間狀態
*/
class Solution {
public int rob(TreeNode root) {
return robSub(root, new HashMap());
}
private int robSub(TreeNode root, Map<TreeNode, Integer> map) {
if (root == null) return 0;
if (map.containsKey(root)) return map.get(root);
int val = 0;
if (root.left != null) val += robSub(root.left.left, map) + robSub(root.left.right, map);
if (root.right != null) val += robSub(root.right.left, map) + robSub(root.right.right, map);
val = Math.max(val + root.val, robSub(root.left, map) + robSub(root.right, map));
map.put(root, val);
return val;
}
}
解法3
/*
當前節點存在偷與不偷兩種情況, 於是可以使用一個只有兩個元素的陣列來儲存每個節點的狀態
res[0]為不偷, res[1]為偷
*/
class Solution {
public int rob(TreeNode root) {
int[] res = robSub(root);
return Math.max(res[0], res[1]);
}
private int[] robSub(TreeNode root) {
if (root == null) return new int[2];
int[] left = robSub(root.left);
int[] right = robSub(root.right);
int[] res = new int[2];
res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
res[1] = root.val + left[0] + right[0];
return res;
}
}
相關文章
- Leetcode 198 House RobberLeetCode
- Leetcode 213 House Robber IILeetCode
- Hackable: III
- LeetCode 動態規劃 House Robber 習題LeetCode動態規劃
- Reflective Journal III
- Path Sum III
- Diablo III ZOJ - 3769
- P110 III
- A - 卡牌遊戲 III遊戲
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- 437-Path Sum III
- 260-Single Number III
- 劍指 Offer 32 - III. 從上到下列印二叉樹 III二叉樹
- [LeetCode] 3163. String Compression IIILeetCode
- vulnhub靶場之HACKABLE: III
- 「譯」MotionLayout介紹 (part III)
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- 力扣 170. 兩數之和 III - 資料結構設計 two-sum III力扣資料結構
- 理光gr3 GR III 替代
- 437. 路徑總和 III
- SAP HUM 巢狀HU初探 III巢狀
- 42-best-time-to-buy-and-sell-stock-iii 力扣 123. 買賣股票的最佳時機 III力扣
- CAT Caterpillar ET Diagnostic Adapter III User ExperienceAPT
- 【做題記錄】ds合集 Part III
- Leetcode——437. 路徑總和 IIILeetCode
- [JRKSJ R9] 在相思樹下 III
- Oracle 20C Concepts(Part III-1)Oracle
- Oracle 20C Concepts(Part III-2)Oracle
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- leetcode best-time-to-buy-and-sell-stock-iii(Java)LeetCodeJava
- Leetcode 1375. Bulb Switcher III (python+cpp)LeetCodePython
- 【Ynoi 2019 模擬賽】Yuno loves sqrt technology III
- SAP RETAIL 特徵引數檔案(Characteristic Profile) IIIAI特徵
- leetcode 123 買賣股票的最佳時機 IIILeetCode
- UcOs-III 原始碼閱讀: os_q.c原始碼
- UcOs-III 原始碼閱讀: os_task.c原始碼
- UcOs-III 原始碼閱讀: os_stat.c原始碼
- 力扣之反轉字串中的單詞 III力扣字串