Skip to content

Commit

Permalink
✨ House Robber 3
Browse files Browse the repository at this point in the history
  • Loading branch information
tangweikun committed Mar 31, 2018
1 parent 205a800 commit 94b1e67
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ npm run test // also you can run `npm run watch`
| 192 | [MaxAreaOfIsLand](src/maxAreaOfIsLand/index.ts) | [:green_book:](src/maxAreaOfIsLand/README.md) | Easy | [LeetCode](https://leetcode.com/problems/max-area-of-island) | DFS |
| 193 | [FloodFill](src/floodFill/index.ts) | [:green_book:](src/floodFill/README.md) | Easy | [LeetCode](https://leetcode.com/problems/flood-fill) | DFS |
| 194 | [BalancedBinaryTree](src/trees/balancedBinaryTree/index.ts) | [:green_book:](src/trees/balancedBinaryTree/README.md) | Easy | [LeetCode](https://leetcode.com/problems/balanced-binary-tree) | DFS |
| 195 | [FriendCircles](src/friendCircles/index.ts) | [:green_book:](src/friendCircles/README.md) | Easy | [LeetCode](https://leetcode.com/problems/friend-circles) | DFS |
| 195 | [FriendCircles](src/friendCircles/index.ts) | [:green_book:](src/friendCircles/README.md) | Medium | [LeetCode](https://leetcode.com/problems/friend-circles) | DFS |
| 196 | [HouseRobber3](src/houseRobber3/index.ts) | [:green_book:](src/houseRobber3/README.md) | Medium | [LeetCode](https://leetcode.com/problems/house-robber-iii) | DFS |

## Collaborators

Expand Down
35 changes: 35 additions & 0 deletions src/trees/houseRobber3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 196.House Robber III

## Description

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.

## Example1

```javascript
3
/ \
2 3
\ \
3 1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
```

## Example2

```javascript
3
/ \
4 5
/ \ \
1 3 1

Maximum amount of money the thief can rob = 4 + 5 = 9.
```

## From

[LeetCode](https://leetcode.com/problems/house-robber-iii)
15 changes: 15 additions & 0 deletions src/trees/houseRobber3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// HELP:

import { I_TreeNode } from './../../_interface/index'

export const rob = (root: I_TreeNode) => {
const money = dfs(root)
return Math.max(money[0], money[1])
}

function dfs(root: I_TreeNode): number[] {
if (!root) return [0, 0]
const L = dfs(root.left)
const R = dfs(root.right)
return [Math.max(L[0], L[1]) + Math.max(R[0], R[1]), root.val + L[0] + R[0]]
}
22 changes: 22 additions & 0 deletions src/trees/houseRobber3/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { rob } from '.'
import { TREE_1, TREE_3, TREE_5, TREE_7, TREE_9 } from '../_testData'

test('HouseRobber3', () => {
expect(rob(TREE_1)).toBe(8)
})

test('HouseRobber3', () => {
expect(rob(TREE_3)).toBe(23)
})

test('HouseRobber3', () => {
expect(rob(TREE_5)).toBe(0)
})

test('HouseRobber3', () => {
expect(rob(TREE_7)).toBe(18)
})

test('HouseRobber3', () => {
expect(rob(TREE_9)).toBe(4)
})
1 change: 1 addition & 0 deletions src/trees/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './twoSum4'
export * from './maximumDepthOfBinaryTree'
export * from './findLargestvalueInEachTreeRow'
export * from './balancedBinaryTree'
export * from './houseRobber3'

0 comments on commit 94b1e67

Please sign in to comment.