Last active
March 19, 2019 13:37
-
-
Save kkchu791/057d0f914357de5b95d95c58e8d12cfc to your computer and use it in GitHub Desktop.
another solution in linear time
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
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