Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxProfit(self, prices: List[int]) -> int:
- """
- Returns the maximum profit earned
- Arguments:
- prices:
- Returns:
- 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
- """
- min_price = float('inf')
- max_profit = float('-inf')
- for i in range(len(prices)):
- # check for minimum price
- if prices[i] < min_price:
- min_price = prices[i]
- # check condition for max profit
- max_profit_sell_today = prices[i] - min_price
- if max_profit_sell_today > max_profit:
- max_profit = max_profit_sell_today
- return max_profit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement