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 66131a1 commit da3ac5dCopy full SHA for da3ac5d
1 file changed
house-robber/ohgyulim.java
@@ -0,0 +1,18 @@
1
+class Solution {
2
+ /* 시간 복잡도: O(N)
3
+ * - for 루프: O(N)
4
+ * 공간 복잡도: O(N), dp배열
5
+ */
6
+ public int rob(int[] nums) {
7
+ int n = nums.length;
8
+ int[] dp = new int[n];
9
+ dp[0] = nums[0];
10
+ if (n > 1) dp[1] = Math.max(nums[0], nums[1]);
11
+ for (int i = 2; i < n; i++) {
12
+ dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
13
+ }
14
+
15
+ return dp[n - 1];
16
17
+}
18
0 commit comments