Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var rob = function(nums) {
- if (nums.length <= 2) {
- return Math.max(...nums, 0);
- }
- const cache = [nums[0], nums[1]];
- const dfs = (i) => {
- const nextPositions = [i + 2, i + 3];
- nextPositions.forEach(nextPos => {
- if (nextPos > nums.length - 1) {
- return;
- }
- const nextVal = cache[i] + nums[nextPos];
- if (cache[nextPos] === undefined || nextVal > cache[nextPos]) {
- cache[nextPos] = nextVal;
- dfs(nextPos);
- }
- });
- }
- dfs(0);
- dfs(1);
- return Math.max(...cache);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement