Skip to content

Commit

Permalink
week10 mission house-robber-ii
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jonghoonpark committed Jul 3, 2024
1 parent eee2a92 commit e11a20f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions house-robber-ii/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- ๋ฌธ์ œ: https://leetcode.com/problems/house-robber-ii/
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/03/leetcode-213

## ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ํ’€์ด

```java
public class Solution {
public int rob(int[] nums) {
if (nums.length == 1) {
return nums[0];
}

if (nums.length == 2) {
return Math.max(nums[0], nums[1]);
}

if (nums.length == 3) {
return Math.max(nums[2], Math.max(nums[0], nums[1]));
}


return Math.max(getMaxInRange(nums, 0, nums.length - 1), getMaxInRange(nums, 1, nums.length));
}

public int getMaxInRange(int[] nums, int start, int end) {
int[] dp = new int[nums.length];
int max;
dp[start] = nums[start];
dp[start + 1] = nums[start + 1];
dp[start + 2] = nums[start + 2] + nums[start];
max = Math.max(dp[start + 2], dp[start + 1]);
for (int i = start + 3; i < end; i++) {
dp[i] = Math.max(nums[i] + dp[i - 2], nums[i] + dp[i - 3]);
max = Math.max(max, dp[i]);
}
return max;
}
}
```

### TC, SC

์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n) ์ด๋‹ค.

0 comments on commit e11a20f

Please sign in to comment.