8 lines of Python codes, 1 pass


  • 0
    J
    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            profit = 0
            buy = float('inf')
            for price in prices:
                if price > buy:
                    profit = max (price - buy, profit)
                else:
                    buy = price
            return profit
    

Log in to reply
 

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