Clean O(n) python code

  • 0
    class Solution(object):
        def maxProfit(self, prices):
            minPrice, maxPrice, profit = None, None, 0
            for p in prices:
                if minPrice is None or p < minPrice:
                    minPrice = maxPrice = p
                elif p > maxPrice:
                    maxPrice = p
                profit = max(profit, maxPrice - minPrice)
            return profit

Log in to reply

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