When we have 3
houses it becomes a little less intuitive and this is where the dynamic programming comes in. Particularly the bottom up(iterative) process. If we are searching for the nth
answer, we can solve a smaller subproblem for the ith
answer to solve for n.
xxxxxxxxxx
let arr = new Array(len);
arr[0] = nums[0];
arr[1] = Math.max(nums[0], nums[1]);
for (let i = 2; i < len; i++) {
arr[i] = Math.max(arr[i - 2] + nums[i], arr[i - 1]);
}
return arr[arr.length - 1];
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment