Skip to content

Commit

Permalink
[Tree] Add a solution to House Robber III
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed May 5, 2018
1 parent dcc3eaa commit 7486c2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 246 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 247 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand Down Expand Up @@ -162,8 +162,9 @@
[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| [Swift](./Tree/ConstructBinaryTreeInorderPostorder.swift)| Medium| O(n)| O(n)|
[Path Sum](https://leetcode.com/problems/path-sum/)| [Swift](./Tree/PathSum.swift)| Easy| O(n)| O(n)|
[Path Sum II](https://leetcode.com/problems/path-sum-ii/)| [Swift](./Tree/PathSumII.swift)| Medium| O(n)| O(n)|
[Path Sum III](https://leetcode.com/problems/path-sum-iiI/)| [Swift](./Tree/PathSumIII.swift)| Easy| O(n^2)| O(1)|
[Path Sum III](https://leetcode.com/problems/path-sum-iii/)| [Swift](./Tree/PathSumIII.swift)| Easy| O(n^2)| O(1)|
[Bnary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Swift](./Tree/BnaryTreePaths.swift)| Easy| O(n)| O(n)|
[House Robber III](https://leetcode.com/problems/house-robber-iii/)| [Swift](./Tree/HouseRobberIII.swift)| Medium| O(n)| O(1)|
[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)| [Swift](./Tree/UniqueBinarySearchTrees.swift)| Medium| O(n^2)| O(n)|
[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)| [Swift](./Tree/RecoverBinarySearchTree.swift)| Hard| O(n)| O(1)|
[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Swift](./Tree/MergeTwoBinaryTrees.swift) | Easy | O(n) | O(n) |
Expand Down Expand Up @@ -484,7 +485,7 @@
| [Swift](./String/LongestSubstringMostKDistinctCharacters.swift) | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) ♥ | Hard
| [Swift](./DP/NestedListWeightSum.swift) | 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) ♥ | Easy
| [Swift](./Math/CountingBits.swift) | 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | Medium
| | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | Medium
| [Swift](./Tree/HouseRobberIII.swift) | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | Medium
| | 336 | [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | Hard
| | 335 | [Self Crossing](https://leetcode.com/problems/self-crossing/) | Hard
| | 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | Medium
Expand Down
40 changes: 40 additions & 0 deletions Tree/HouseRobberIII.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Question Link: https://leetcode.com/problems/house-robber-iii/
* Primary idea: Using two sums to track rob sum starting from current node or not,
* compare and get the maximum one
* Time Complexity: O(n), Space Complexity: O(1)
*
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/

class HouseRobberIII {
func rob(_ root: TreeNode?) -> Int {
let (robRoot, notRobRoot) = helper(root)

return max(robRoot, notRobRoot)
}

fileprivate func helper(_ node: TreeNode?) -> (Int, Int) {
guard let node = node else {
return (0, 0)
}

let (robLeft, notRobLeft) = helper(node.left)
let (robRight, notRobRight) = helper(node.right)

let robNode = notRobLeft + notRobRight + node.val
let notRobNode = max(robLeft, notRobLeft) + max(robRight, notRobRight)

return (robNode, notRobNode)
}
}

0 comments on commit 7486c2b

Please sign in to comment.