Skip to content

Commit 8414df7

Browse files
committed
feat: house-robber
1 parent bd8eb04 commit 8414df7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/choidabom.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/house-robber/
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
6+
function rob(nums: number[]): number {
7+
if (nums.length === 1) return nums[0];
8+
if (nums.length === 2) return Math.max(nums[0], nums[1]);
9+
10+
const dp: number[] = [];
11+
dp[0] = nums[0];
12+
dp[1] = Math.max(nums[0], nums[1]);
13+
14+
for (let i = 2; i < nums.length; i++) {
15+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
16+
}
17+
18+
return dp[nums.length - 1];
19+
}

0 commit comments

Comments
 (0)