[Swift]LeetCode337. 打家劫舍 III | House Robber III

[Swift]LeetCode337. 打家劫舍 III | House Robber III,第1张

概述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 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,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,1]     3    /    4   5  / \   \  1   3   1Output: 9Explanation: Maximum amount of money the thIEf can rob = 4 + 5 = 9.

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

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

示例 1:

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

示例 2:

输入: [3,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 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode337. 打家劫舍 III | House Robber III全部内容,希望文章能够帮你解决[Swift]LeetCode337. 打家劫舍 III | House Robber III所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1020700.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存