121. Best Time to Buy and Sell Stock

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

你可能感兴趣的:(121. Best Time to Buy and Sell Stock)