Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxProfit(self, prices: List[int]) -> int:
- min_price_so_far = float("inf")
- max_profit = -float("inf")
- for i in range(len(prices)):
- if prices[i] - min_price_so_far >=0:
- max_profit_so_far = prices[i] - min_price_so_far
- max_profit = max(max_profit, max_profit_so_far)
- min_price_so_far = min(prices[i], min_price_so_far)
- # also care for edge case when low prices are never observed
- return max_profit if max_profit>0 else 0
Advertisement
Add Comment
Please, Sign In to add comment