Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Created March 19, 2019 13:21
Show Gist options
  • Save kkchu791/34b741639934b888fb143639caf3cfe7 to your computer and use it in GitHub Desktop.
Save kkchu791/34b741639934b888fb143639caf3cfe7 to your computer and use it in GitHub Desktop.
linear time solution
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