Created
March 25, 2019 18:50
-
-
Save ykdojo/dafa470fdc9606a35be83947ef31f5f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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