From 4fa39a5a47708e9a7cb13cf664acbbfc4a3d5a7b Mon Sep 17 00:00:00 2001 From: "ningwei.shi" Date: Tue, 10 Nov 2020 15:32:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8A=A2=E6=88=BF=E5=AD=90Python3=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\212\242\346\210\277\345\255\220.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" index 8dae329bc6..ed5006086c 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" @@ -259,3 +259,63 @@ int[] dp(TreeNode root) {

+[Shantom](https://github.com/Shantom) 提供 198. House Robber I Python3 解法代码: + +```Python +class Solution: + def rob(self, nums: List[int]) -> int: + # 当前,上一间,上上间 + cur, pre1, pre2 = 0, 0, 0 + + for num in nums: + # 当前 = max(上上间+(抢当前),上间(放弃当前)) + cur = max(pre2 + num, pre1) + pre2 = pre1 + pre1 = cur + + return cur +``` +[Shantom](https://github.com/Shantom) 提供 213. House Robber II Python3 解法代码: + +```Python +class Solution: + def rob(self, nums: List[int]) -> int: + # 只有一间时不成环 + if len(nums) == 1: + return nums[0] + + # 该函数同198题 + def subRob(nums: List[int]) -> int: + # 当前,上一间,上上间 + cur, pre1, pre2 = 0, 0, 0 + for num in nums: + # 当前 = max(上上间+(抢当前),上间(放弃当前)) + cur = max(pre2 + num, pre1) + pre2 = pre1 + pre1 = cur + return cur + + # 不考虑第一间或者不考虑最后一间 + return max(subRob(nums[:-1]), subRob(nums[1:])) +``` +[Shantom](https://github.com/Shantom) 提供 337. House Robber III Python3 解法代码: + +```Python +class Solution: + def rob(self, root: TreeNode) -> int: + # 返回值0项为不抢该节点,1项为抢该节点 + def dp(root): + if not root: + return 0, 0 + + left = dp(root.left) + right = dp(root.right) + + do = root.val + left[0] + right[0] + do_not = max(left) + max(right) + + return do_not, do + + return max(dp(root)) +``` + From 8a2f374f63d568d3e45d99a6c0c4466d399ea2d5 Mon Sep 17 00:00:00 2001 From: "ningwei.shi" Date: Thu, 12 Nov 2020 09:21:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\212\242\346\210\277\345\255\220.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" index d2c3f0f7a6..081c583ac2 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" @@ -311,7 +311,9 @@ class Solution: left = dp(root.left) right = dp(root.right) + # 抢当前,则两个下家不抢 do = root.val + left[0] + right[0] + # 不抢当前,则下家随意 do_not = max(left) + max(right) return do_not, do