forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
213 House Robber II.js
39 lines (30 loc) · 1.39 KB
/
213 House Robber II.js
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
34
35
36
37
38
39
// Note: This is an extension of House Robber.
// 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.
// Microsoft
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
// same as house robber I, but we cannot rob first and last house at the same time.
function robHouses(nums) {
var odd = 0;
var even = 0;
for(var i = 0; i < nums.length; i++) {
var num = nums[i];
if(i % 2 === 0) {
even = Math.max(even + num, odd);
} else {
odd = Math.max(odd + num, even);
}
}
return Math.max(even, odd);
}
if(nums.length <= 1) {
return robHouses(nums);
}
var robHousesExceptLast = robHouses(nums.slice(0, -1));
var robHousesExceptFirst = robHouses(nums.slice(1));
return Math.max(robHousesExceptLast, robHousesExceptFirst);
};