Simple Python solution


  • 0
    Y
    class Solution(object):
        def maxProfit(self, prices):
            if len(prices) <= 1: return 0
            
            min_price, max_profit = prices[0], 0
            for i in range(1, len(prices)):
                max_profit = max(max_profit, prices[i] - min_price)
                min_price = min(min_price, prices[i])
            
            return max_profit
    
    # Time: O(n), space: O(1)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.