Advertisement
lazar955

Untitled

Oct 24th, 2023
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.39 KB | None | 0 0
  1. func maxProfit(prices []int) int {
  2.     l,r:=0,1 // buy, sell
  3.     maxP:=0
  4.  
  5.     for r < len(prices) {
  6.         if prices[l] < prices[r] {
  7.             profit := prices[r] - prices[l]
  8.             maxP = max(maxP, profit)
  9.         }else {
  10.             l = r
  11.         }
  12.         r +=1
  13.     }
  14.     return maxP
  15. }
  16.  
  17. func max(l,r int )int {
  18.     if l > r {
  19.         return l
  20.     }
  21.     return r
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement