forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path198 House Robber.js
More file actions
26 lines (23 loc) · 530 Bytes
/
Copy path198 House Robber.js
File metadata and controls
26 lines (23 loc) · 530 Bytes
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
// Leetcode #198
// Language: Javascript
// Problem: https://leetcode.com/problems/house-robber/
// Author: Chihung Yu
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
if(nums === null){
return nums;
}
var even = 0;
var odd = 0;
for(var i = 0; i < nums.length; i++){
if(i%2 === 0){
even = Math.max(even + nums[i], odd);
} else {
odd = Math.max(odd + nums[i], even);
}
}
return Math.max(even,odd);
};