Skip to content

Commit 5e33f02

Browse files
committed
house-robber solution
1 parent 044cdcf commit 5e33f02

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/Yu-Won.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/house-robber/description/
3+
*
4+
* 요구사항:
5+
* nums: number[]를 Input 으로 받았을 때
6+
* 인접하지 않은 데이터들의 합 중 가장 큰 값을 리턴한다.
7+
*
8+
* * */
9+
10+
const houseRobber = (nums) => {
11+
let prev1 = 0;
12+
let prev2 = 0;
13+
for(let i = 0; i < nums.length; i++) {
14+
let current = Math.max(nums[i] + prev2, prev1);
15+
prev2 = prev1;
16+
prev1 = current;
17+
}
18+
return prev1;
19+
}

0 commit comments

Comments
 (0)