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 2577233 commit dc07507Copy full SHA for dc07507
house-robber/eunice-hong.ts
@@ -0,0 +1,16 @@
1
+/**
2
+ * Finds the maximum amount of money that can be robbed without robbing two adjacent houses.
3
+ * Uses dynamic programming to track the best outcome at each step.
4
+ *
5
+ * @param {number[]} nums
6
+ * @return {number}
7
+ */
8
+function rob(nums: number[]): number {
9
+ const dp = new Array(nums.length + 1);
10
+ dp[0] = 0;
11
+ dp[1] = nums[0];
12
+ for (let i = 2; i < dp.length; i++) {
13
+ dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
14
+ }
15
+ return dp[dp.length - 1];
16
+};
0 commit comments