forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205a800
commit 94b1e67
Showing
5 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters