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 0c6f969 commit 582f532Copy full SHA for 582f532
βhouse-robber/sunjae95.jsβ
@@ -0,0 +1,22 @@
1
+/**
2
+ * @description
3
+ * μ΅λν λ§μ μμ λμ΄λΌλ 문ꡬμμ dynamic programmingμ μ°μ
4
+ * μ°μλ μ§μ νΈ μ μλ€λΌλ 문ꡬμμ μ νμμ λμΆ ν μ μμμ
5
+ *
6
+ * n = length of nums
7
+ * time complexity: O(n)
8
+ * space complexity: O(n)
9
+ */
10
+var rob = function (nums) {
11
+ if (nums.length === 1) return nums[0];
12
+
13
+ const dp = Array(nums.length).fill(0);
14
15
+ dp[0] = nums[0];
16
+ dp[1] = Math.max(nums[1], dp[0]);
17
18
+ for (let i = 2; i < nums.length; i++)
19
+ dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
20
21
+ return dp[nums.length - 1];
22
+};
0 commit comments