Skip to content

Instantly share code, notes, and snippets.

@ykdojo
Created March 25, 2019 18:50
Show Gist options
  • Save ykdojo/dafa470fdc9606a35be83947ef31f5f7 to your computer and use it in GitHub Desktop.
Save ykdojo/dafa470fdc9606a35be83947ef31f5f7 to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
let memo = Array(nums.length);
return helper(nums, 0, memo);
};
var helper = function(nums, current, memo) {
if (current > nums.length) {
return 0;
}
if (memo[current] !== undefined) {
return memo[current];
}
let option1 = nums[current] + helper(nums, current + 2, memo);
let option2 = helper(nums, current + 1, memo);
let result;
if (option1 >= option2) {
result = option1;
}
else {
result = option2;
}
memo[current] = result;
return memo[current];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment