Skip to content

Commit eee2a92

Browse files
week10 mission house-robber
1 parent eb7476e commit eee2a92

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

โ€Žhouse-robber/dev-jonghoonpark.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/house-robber/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/03/leetcode-198
3+
4+
## ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ํ’€์ด
5+
6+
```java
7+
public class Solution {
8+
public int rob(int[] nums) {
9+
int[] dp = new int[nums.length];
10+
int max = 0;
11+
12+
if (nums.length == 1) {
13+
return nums[0];
14+
}
15+
16+
if (nums.length == 2) {
17+
return Math.max(nums[0], nums[1]);
18+
}
19+
20+
if (nums.length == 3) {
21+
return Math.max(nums[2] + nums[0], nums[1]);
22+
}
23+
24+
dp[0] = nums[0];
25+
dp[1] = nums[1];
26+
dp[2] = nums[2] + nums[0];
27+
max = Math.max(dp[2], dp[1]);
28+
for (int i = 3; i < nums.length; i++) {
29+
dp[i] = Math.max(nums[i] + dp[i - 2], nums[i] + dp[i - 3]);
30+
max = Math.max(max, dp[i]);
31+
}
32+
return max;
33+
}
34+
}
35+
```
36+
37+
### TC, SC
38+
39+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n) ์ด๋‹ค.

0 commit comments

Comments
ย (0)