Skip to content

Commit 324485e

Browse files
committed
solve : DaleStudy#264 (House Robber) with Java
1 parent 4ac4481 commit 324485e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

house-robber/kdh-92.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
// (1) dp
4+
// int n = nums.length;
5+
6+
// if (n == 1) return nums[0];
7+
8+
// int[] dp = new int[n];
9+
10+
// dp[0] = nums[0];
11+
// dp[1] = Math.max(nums[0], nums[1]);
12+
13+
// for (int i = 2; i < n; i++) {
14+
// dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
15+
// }
16+
17+
// return dp[n - 1];
18+
19+
// (2) 인접 값 비교
20+
int prev = 0, curr = 0;
21+
for (int num : nums) {
22+
int temp = curr;
23+
curr = Math.max(num + prev, curr);
24+
prev = temp;
25+
}
26+
27+
return curr;
28+
}
29+
}

top-k-frequent-elements/kdh-92.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ public int[] topKFrequent(int[] nums, int k) {
7979

8080
return result.stream().mapToInt(Integer::intValue).toArray();
8181
}
82-
}
82+
}

0 commit comments

Comments
 (0)