Skip to content

Commit cb1f1d1

Browse files
committed
feat: add House Robber
1 parent 0f0e429 commit cb1f1d1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Roadmap: https://neetcode.io/roadmap
8181
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) | Medium | [ts](./TypeScript/17.letter-combinations-of-a-phone-number.ts) | Backtracking |
8282
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/description/) | Easy | [ts](./TypeScript/70.climbing-stairs.ts) | 1-D DP |
8383
| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/) | Easy | [ts](./TypeScript/70.climbing-stairs.ts) | 1-D DP |
84+
| 198 | [House Robber](https://leetcode.com/problems/house-robber/description/) | Medium | [ts](./TypeScript/70.climbing-stairs.ts) | 1-D DP |
8485

8586
### Others
8687

TypeScript/198.house-robber.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function rob(nums: number[]): number {
2+
for (let i = 1; i < nums.length; i++) {
3+
nums[i] =
4+
i === 1
5+
? Math.max(nums[1], nums[0])
6+
: Math.max(nums[i - 2] + nums[i], nums[i - 1]);
7+
}
8+
9+
return nums[nums.length - 1];
10+
}
11+
12+
console.log(rob([2, 7, 9, 3, 1]));
13+
console.log(rob([2, 1, 1, 2]));

0 commit comments

Comments
 (0)