Skip to content

Commit acd83cb

Browse files
committed
Added House Robber II.java
1 parent 886c9a1 commit acd83cb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Medium/House Robber II.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums.length == 1) {
4+
return nums[0];
5+
}
6+
return Math.max(robHelper(nums, 0, nums.length - 2), robHelper(nums, 1, nums.length - 1));
7+
}
8+
9+
private int robHelper(int[] nums, int lower, int higher) {
10+
int include = 0;
11+
int exclude = 0;
12+
for (int j = lower; j <= higher; j++) {
13+
int includeIdx = include;
14+
int excludeIdx = exclude;
15+
include = excludeIdx + nums[j];
16+
exclude = Math.max(excludeIdx, includeIdx);
17+
}
18+
return Math.max(include, exclude);
19+
}
20+
}

0 commit comments

Comments
 (0)