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 e56df25 commit 976cf28Copy full SHA for 976cf28
1 file changed
house-robber/robinyoon-dev.js
@@ -0,0 +1,22 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+var rob = function (nums) {
6
+
7
+ const NUMS_LENGTH = nums.length;
8
9
+ if (NUMS_LENGTH === 0) return 0;
10
+ if (NUMS_LENGTH === 1) return nums[0];
11
12
+ const maxSumsArr = new Array(NUMS_LENGTH);
13
+ maxSumsArr[0] = nums[0];
14
+ maxSumsArr[1] = Math.max(nums[0], nums[1]);
15
16
+ for (let i = 2; i < NUMS_LENGTH; i++) {
17
+ maxSumsArr[i] = Math.max(maxSumsArr[i - 2] + nums[i], maxSumsArr[i - 1]);
18
+ }
19
20
+ return maxSumsArr[NUMS_LENGTH - 1];
21
22
+};
0 commit comments