Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const maxProfit = prices => {
- let profit = 0;
- let start = 0,
- end = 0;
- // sum of all ascendent subsequences
- for (let i = 1; i < prices.length; i++) {
- if (prices[i] > prices[i - 1]) {
- end = i;
- } else {
- profit += prices[end] - prices[start];
- start = i;
- end = i;
- }
- }
- if (end === prices.length - 1) profit += prices[end] - prices[start];
- return profit;
- };
Add Comment
Please, Sign In to add comment