213 House Robber II
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle.That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
思路不是很明白:
用两个范围做,因为是circle,第一种是index from 0 to len - 2
index from 1 to len - 1
find the max
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0 ){
return 0;
}
if (nums.length == 1) {
return nums[0];
}
int includeFirst = helper(nums, 0, nums.length - 2);
int excludeFirst = helper(nums, 1, nums.length - 1);
return Math.max(includeFirst,excludeFirst);
}
public int helper(int[] nums, int start, int end) {
int prevYes = 0;
int prevNo = 0;
for (int i = start; i <= end; i++) {
int temp = prevNo;
prevNo = Math.max(prevYes, prevNo);
prevYes = temp + nums[i];
}
return Math.max(prevYes, prevNo);
}
}