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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-nbjqjdir-hr.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.node

Determine the maximum amount of money the thief can rob tonight without alerting the police.git

Example 1:github

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.

在上次打劫完一條街道以後和一圈房屋後,小偷又發現了一個新的可行竊的地區。這個地區只有一個入口,咱們稱之爲「根」。 除了「根」以外,每棟房子有且只有一個「父「房子與之相連。一番偵察以後,聰明的小偷意識到「這個地方的全部房屋的排列相似於一棵二叉樹」。 若是兩個直接相連的房子在同一天晚上被打劫,房屋將自動報警。ide

計算在不觸動警報的狀況下,小偷一晚可以盜取的最高金額。this

示例 1:spa

輸入: [3,2,3,null,3,null,1]

     3
    / \
   2   3
    \   \ 
     3   1

輸出: 7 
解釋: 小偷一晚可以盜取的最高金額 = 3 + 3 + 1 = 7.

示例 2:code

輸入: [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 }

56msorm

 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 }
相關文章
相關標籤/搜索