Skip to content

Commit 58283f9

Browse files
committed
[juhui-jeong] WEEK 14 Solutions
1 parent 9f8c56e commit 58283f9

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

house-robber-ii/juhui-jeong.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
int n = nums.length;
4+
if (n == 1) return nums[0];
5+
if (n == 2) return Math.max(nums[0], nums[1]);
6+
7+
return Math.max(robRange(nums, 0, n - 2), robRange(nums, 1, n - 1));
8+
}
9+
10+
private int robRange(int[] nums, int start, int end) {
11+
int prev2 = 0;
12+
int prev1 = 0;
13+
14+
for (int i = start; i <= end; i++) {
15+
int cur = Math.max(prev1, prev2 + nums[i]);
16+
prev2 = prev1;
17+
prev1 = cur;
18+
}
19+
return prev1;
20+
}
21+
}

0 commit comments

Comments
 (0)