My Concise Python Solution 52ms


  • 1
    M
    class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        
        """
        if not prices: return 0
        buy = prices[0]
        profit = 0
        for i in xrange(1,len(prices)):
            if prices[i] < buy:
                buy = prices[i]
            profit = max(profit, prices[i] -buy)
        return profit

  • 0
    M

    Time Complexity O(n) Space Complexity O(1)


Log in to reply
 

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