Created
March 19, 2019 13:21
-
-
Save kkchu791/34b741639934b888fb143639caf3cfe7 to your computer and use it in GitHub Desktop.
linear time solution
This file contains hidden or 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(profits) { | |
let prevMaxProfit = 0; | |
let currMaxProfit = 0; | |
for (let i = 0; i < profits.length; i++) { | |
const prevPlusProfit = prevMaxProfit + profits[i]; | |
prevMaxProfit = currMaxProfit; | |
currMaxProfit = Math.max(currMaxProfit, prevPlusProfit); | |
} | |
return currMaxProfit; | |
}; | |
rob([2, 1, 1, 2, 1, 2]) | |
// output = 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment