Buying and Selling Stock

Given a list of starting and ending days for a stock, maximize your profit Tags: Greedy

Try It!

Discussion

Video

Solution

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        lowest = prices[0]
        max_gain = 0
        for i in range(1, len(prices)):
            price = prices[i]
            if price - lowest > max_gain:
                max_gain = price - lowest
            if price < lowest:
                lowest = price
        return max_gain