Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxProfit(self, prices: List[int]) -> int:
- """
- Notes
- -----
- The problem translates to finding maximum profit which is
- difference of the current price and minimum seen so far.
- It is an optimisation over the brute-force aproach where
- one does not need to determine minimum so far everytime
- """
- max_index_so_far = -1
- min_index_so_far = -1
- min_so_far = math.inf
- max_so_far= -math.inf
- left, right = 0, 0
- max_profit = 0
- for i in range(len(prices)):
- if min_so_far >= prices[i]:
- min_index_so_far = i
- min_so_far = prices[i]
- profit = prices[i] - min_so_far
- if profit > max_profit:
- max_profit = profit
- return max_profit
Advertisement
Add Comment
Please, Sign In to add comment