Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. var rob = function(nums) {
  2. if (nums.length <= 2) {
  3. return Math.max(...nums, 0);
  4. }
  5.  
  6. const cache = [nums[0], nums[1]];
  7.  
  8. const dfs = (i) => {
  9. const nextPositions = [i + 2, i + 3];
  10. nextPositions.forEach(nextPos => {
  11. if (nextPos > nums.length - 1) {
  12. return;
  13. }
  14.  
  15. const nextVal = cache[i] + nums[nextPos];
  16. if (cache[nextPos] === undefined || nextVal > cache[nextPos]) {
  17. cache[nextPos] = nextVal;
  18. dfs(nextPos);
  19. }
  20. });
  21. }
  22.  
  23. dfs(0);
  24. dfs(1);
  25.  
  26. return Math.max(...cache);
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement