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 0e1484d commit b6fa871Copy full SHA for b6fa871
house-robber/jaejeong1.java
@@ -0,0 +1,20 @@
1
+class Solution {
2
+ public int rob(int[] nums) {
3
+ // 인접한 경우는 제외한 최대 합
4
+ // 풀이: dp로 풀이한다. dp[i] = max(dp[i-1], dp[i-2] + nums[i])
5
+ // TC: O(N), SC: O(N)
6
+ if (nums.length == 1) {
7
+ return nums[0];
8
+ }
9
+
10
+ var dp = new int[nums.length];
11
+ dp[0] = nums[0];
12
+ dp[1] = Math.max(nums[0], nums[1]);
13
14
+ for (int i=2; i<nums.length; i++) {
15
+ dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);
16
17
18
+ return dp[nums.length-1];
19
20
+}
0 commit comments