Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. const maxProfit = prices => {
  2. let profit = 0;
  3. let start = 0,
  4. end = 0;
  5.  
  6. // sum of all ascendent subsequences
  7. for (let i = 1; i < prices.length; i++) {
  8. if (prices[i] > prices[i - 1]) {
  9. end = i;
  10. } else {
  11. profit += prices[end] - prices[start];
  12. start = i;
  13. end = i;
  14. }
  15. }
  16.  
  17. if (end === prices.length - 1) profit += prices[end] - prices[start];
  18.  
  19. return profit;
  20. };
Add Comment
Please, Sign In to add comment