Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def maxProfit(self, prices: List[int]) -> int:
  3.         result = 0
  4.         buy_idx = 0
  5.         sell_idx = 0
  6.         for i in range(1, len(prices)):
  7.             if prices[i] >= prices[sell_idx]:
  8.                 sell_idx = i
  9.             else:
  10.                 if sell_idx > buy_idx:
  11.                     result += prices[sell_idx] - prices[buy_idx]
  12.                 sell_idx = buy_idx = i
  13.         if sell_idx > buy_idx:
  14.             result += prices[sell_idx] - prices[buy_idx]
  15.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement