Skip to content

Commit 09a28de

Browse files
authored
Update 213-house-robber-ii.js
1 parent 9dfc6e1 commit 09a28de

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

213-house-robber-ii.js

+21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ const rob = function(nums) {
2020

2121
// another
2222

23+
/**
24+
* @param {number[]} nums
25+
* @return {number}
26+
*/
27+
const rob = function(nums) {
28+
if(nums.length === 1) return nums[0]
29+
return Math.max(helper(0, nums.length - 2), helper(1, nums.length - 1))
30+
31+
function helper(l, r) {
32+
let inc = 0, exc = 0
33+
for(let i = l; i <= r; i++) {
34+
const pi = inc, pe = exc
35+
inc = exc + nums[i]
36+
exc = Math.max(pi, pe)
37+
}
38+
return Math.max(inc, exc)
39+
}
40+
};
41+
42+
// another
43+
2344
/**
2445
* @param {number[]} nums
2546
* @return {number}

0 commit comments

Comments
 (0)