为有牺牲多壮志,敢教日月换新天。

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

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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   1

Output: 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   1

Output: 9
Explanation: 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 = nil
10  *         self.right = nil
11  *     }
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 = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func rob(_ root: TreeNode?) -> Int {
16         guard let node = root else {
17             return 0
18         }
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 res
31     }
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 = nil
10  *         self.right = nil
11  *     }
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 rob
22         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.val
28         let noRobC = max(left.1,left.0) + max(right.1,right.0)
29                 
30         return (robCur, noRobC)
31     }
32 }

 

posted @ 2019-01-13 10:40  为敢技术  阅读(271)  评论(0编辑  收藏  举报