public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
return Math.max(helper(nums, 0, nums.length - 2), helper(nums, 1, nums.length - 1));
}
private int helper(int[] nums, int start, int end) {
int prevNotRobbed = 0;
int prevRobbed = 0;
for (int i = start; i <= end; i++) {
int currNotRobbed = Math.max(prevNotRobbed, prevRobbed);
int currRobbed = nums[i] + prevNotRobbed;
prevNotRobbed = currNotRobbed;
prevRobbed = currRobbed;
}
return Math.max(prevNotRobbed, prevRobbed);
}
}