Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Last active March 19, 2019 13:37
Show Gist options
  • Save kkchu791/057d0f914357de5b95d95c58e8d12cfc to your computer and use it in GitHub Desktop.
Save kkchu791/057d0f914357de5b95d95c58e8d12cfc to your computer and use it in GitHub Desktop.
another solution in linear time
var rob = function(nums) {
var prevEvenMax = 0;
var prevOddMax = 0;
for (var i = 0; i < nums.length; i++) {
if (i % 2 === 0) {
prevEvenMax = Math.max(prevEvenMax + nums[i], prevOddMax);
}
else {
prevOddMax = Math.max(prevEvenMax, prevOddMax + nums[i]);
}
}
return Math.max(prevEvenMax, prevOddMax);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment