- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouseRobberII.java
More file actions
Latest commit
17 lines (16 loc) · 557 Bytes
/
Copy pathHouseRobberII.java
File metadata and controls
17 lines (16 loc) · 557 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
publicclassHouseRobberII {
publicintrob(int[] nums) {
if(nums.length == 1) returnnums[0];
returnMath.max(robHelper(nums, 0, nums.length - 1), robHelper(nums, 1, nums.length));
}
privateintrobHelper(int[] nums, intstart, intend) {
intinclude = 0;
intexclude = 0;
for(inti = start; i < end; i++) {
intlastInclude = include;
include = exclude + nums[i];
exclude = Math.max(lastInclude, exclude);
}
returnMath.max(include, exclude);
}
}