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 81d1a8f commit 1e5f7daCopy full SHA for 1e5f7da
house-robber-ii/jdy8739.js
@@ -0,0 +1,27 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+var rob = function (nums) {
6
+ if (nums.length === 1) {
7
+ return nums[0];
8
+ }
9
+
10
+ const dp1 = [0, nums[0]];
11
+ const dp2 = [0, 0];
12
13
+ for (let i = 1; i < nums.length; i++) {
14
+ const prevIndex = i - 1;
15
16
+ const dp1Max = Math.max(dp1[prevIndex] + nums[i], dp1[i]);
17
+ dp1.push(dp1Max);
18
19
+ const dp2Max = Math.max(dp2[prevIndex] + nums[i], dp2[i]);
20
+ dp2.push(dp2Max);
21
22
23
+ return Math.max(dp1[dp1.length - 2], dp2[dp2.length - 1])
24
+};
25
26
+// 시간복잡도 O(n) -> nums의 길이만큼 for문에서 최대값을 dp계산
27
+// 공간복잡도 O(n) -> nums의 길이만큼 dp배열에 원소가 추가됨
0 commit comments