We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bd8eb04 commit 8414df7Copy full SHA for 8414df7
house-robber/choidabom.ts
@@ -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