-
Notifications
You must be signed in to change notification settings - Fork 35
/
HouseRobberII.java
33 lines (26 loc) · 1.59 KB
/
HouseRobberII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
*/
public class HouseRobberII {
public int rob(int[] nums) {
if (nums.length == 0)
return 0;
if (nums.length < 2)
return nums[0];
int[] startFromFirstHouse = new int[nums.length + 1];
int[] startFromSecondHouse = new int[nums.length + 1];
startFromFirstHouse[0] = 0;
startFromFirstHouse[1] = nums[0];
startFromSecondHouse[0] = 0;
startFromSecondHouse[1] = 0;
for (int i = 2; i <= nums.length; i++) {
startFromFirstHouse[i] = Math.max(startFromFirstHouse[i - 1], startFromFirstHouse[i - 2] + nums[i-1]);
startFromSecondHouse[i] = Math.max(startFromSecondHouse[i - 1], startFromSecondHouse[i - 2] + nums[i-1]);
}
return Math.max(startFromFirstHouse[nums.length - 1], startFromSecondHouse[nums.length]);
}
}