Skip to content

Commit 39f7e8e

Browse files
author
이호찬
committed
house-robber solution
1 parent 61bb104 commit 39f7e8e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

house-robber/lhc0506.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function(nums) {
6+
if (nums.length < 2) {
7+
return Math.max(nums);
8+
}
9+
10+
let twoBefore = 0;
11+
let oneBefore = 0;
12+
13+
nums.forEach(num => {
14+
const prevOneBefore = oneBefore
15+
oneBefore = Math.max(prevOneBefore, twoBefore + num);
16+
twoBefore = prevOneBefore;
17+
});
18+
19+
return Math.max(oneBefore, twoBefore);
20+
};

0 commit comments

Comments
 (0)