博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode337. 打家劫舍 III | House Robber III
阅读量:5101 次
发布时间:2019-06-13

本文共 4189 字,大约阅读时间需要 13 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址: 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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:

Input: [3,2,3,null,3,null,1]     3    / \   2   3    \   \      3   1Output: 7 Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

Input: [3,4,5,1,3,null,1]     3    / \   4   5  / \   \  1   3   1Output: 9Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.

在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。

计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。

示例 1:

输入: [3,2,3,null,3,null,1]     3    / \   2   3    \   \      3   1输出: 7 解释: 小偷一晚能够盗取的最高金额 = 3 + 3 + 1 = 7.

示例 2:

输入: [3,4,5,1,3,null,1]     3    / \   4   5  / \   \  1   3   1输出: 9解释: 小偷一晚能够盗取的最高金额 = 4 + 5 = 9.

52ms
1 /** 2  * Definition for a binary tree node. 3  * public class TreeNode { 4  *     public var val: Int 5  *     public var left: TreeNode? 6  *     public var right: TreeNode? 7  *     public init(_ val: Int) { 8  *         self.val = val 9  *         self.left = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     func rob(_ root: TreeNode?) -> Int {16         let maxs = robMaxs(root)17 18         return max(maxs.0, maxs.1)19     }20 21     func robMaxs(_ root : TreeNode?) -> (Int, Int) {22         if root == nil {23             return (0,0)24         }25 26         let leftMaxs = robMaxs(root?.left)27         let rightMaxs = robMaxs(root?.right)28         29         30         return (leftMaxs.1 + rightMaxs.1 + root!.val, max(leftMaxs.1 + rightMaxs.1,leftMaxs.0 + rightMaxs.0, leftMaxs.0 + rightMaxs.1, leftMaxs.1 + rightMaxs.0))31     }32 }

56ms

1 /** 2  * Definition for a binary tree node. 3  * public class TreeNode { 4  *     public var val: Int 5  *     public var left: TreeNode? 6  *     public var right: TreeNode? 7  *     public init(_ val: Int) { 8  *         self.val = val 9  *         self.left = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     func rob(_ root: TreeNode?) -> Int {16         guard let node = root else {17             return 018         }19         return max(helper(node)[0], helper(node)[1])20     }21     private func helper(_ root: TreeNode?) -> [Int] {22         guard let node = root else {23             return [0, 0]24         }25         var res = [Int](repeating: 0, count: 2)26         let left = helper(node.left)27         let right = helper(node.right)28         res[0] = node.val + left[1] + right[1]29         res[1] = max(left[0], left[1]) + max(right[0], right[1])30         return res31     }32 }

60ms

1 /** 2  * Definition for a binary tree node. 3  * public class TreeNode { 4  *     public var val: Int 5  *     public var left: TreeNode? 6  *     public var right: TreeNode? 7  *     public init(_ val: Int) { 8  *         self.val = val 9  *         self.left = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     16     func rob(_ root: TreeNode?) -> Int {17         let res = findMax(root)18         return max(res.0, res.1)19     }20     21     func findMax(_ root:TreeNode?) -> (Int, Int){  //do rob root, do not rob22         guard let root = root else {23             return (0,0)24         }25         let left = findMax(root.left)26         let right = findMax(root.right)27         let robCur = left.1 + right.1 + root.val28         let noRobC = max(left.1,left.0) + max(right.1,right.0)29                 30         return (robCur, noRobC)31     }32 }

 

转载于:https://www.cnblogs.com/strengthen/p/10262047.html

你可能感兴趣的文章
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
[Data Structure & Algorithm] 有向无环图的拓扑排序及关键路径
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
Vue音乐项目笔记(三)
查看>>
遍历Map对象
查看>>
计算剪贴板里仿制的代码行数
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
#Leetcode# 209. Minimum Size Subarray Sum
查看>>
SDN第四次作业
查看>>
DM8168 DVRRDK软件框架研究
查看>>
django迁移数据库错误
查看>>
yii 跳转页面
查看>>
洛谷 1449——后缀表达式(线性数据结构)
查看>>