Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement