/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
if (nums.length === 1)
return nums[0];
let first = nums[0];
let second = Math.max(first, nums[1]);
for (let i = 2; i < nums.length; i++) {
const curr = nums[i];
const max = Math.max(first + curr, second);
first = second;
second = max;
}
return second;
};
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
const sum = new Array(nums.length);
sum[0] = nums[0];
sum[1] = Math.max(sum[0], nums[1]);
for (let i = 2; i < nums.length; i++) {
const curr = nums[i];
sum[i] = Math.max(sum[i - 2] + curr, sum[i - 1]);
sum[i] = Math.max(curr, sum[i]);
}
return sum[nums.length - 1];
};