diff --git a/README.md b/README.md index f00b285..b39b70d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/trees/houseRobber3/README.md b/src/trees/houseRobber3/README.md new file mode 100644 index 0000000..e9fe523 --- /dev/null +++ b/src/trees/houseRobber3/README.md @@ -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) diff --git a/src/trees/houseRobber3/index.ts b/src/trees/houseRobber3/index.ts new file mode 100644 index 0000000..ec67144 --- /dev/null +++ b/src/trees/houseRobber3/index.ts @@ -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]] +} diff --git a/src/trees/houseRobber3/test.ts b/src/trees/houseRobber3/test.ts new file mode 100644 index 0000000..d04634e --- /dev/null +++ b/src/trees/houseRobber3/test.ts @@ -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) +}) diff --git a/src/trees/index.ts b/src/trees/index.ts index 8728f6f..c2db3ef 100644 --- a/src/trees/index.ts +++ b/src/trees/index.ts @@ -18,3 +18,4 @@ export * from './twoSum4' export * from './maximumDepthOfBinaryTree' export * from './findLargestvalueInEachTreeRow' export * from './balancedBinaryTree' +export * from './houseRobber3'