Simple python solution, 44ms, 95th percentile


  • 0
    J
    class Solution(object):
    def maxProfit(self, prices):
        if len(prices) == 0:
            return 0
        if len(prices) == 1:
            return 0
        maxp = 0
        minsf, maxsf = prices[0], prices[0] 
        for price in prices:
            if price > maxsf:
                maxsf = price
            if price < minsf:
                minsf = price
                maxsf = price
            if maxsf - minsf > maxp:
                maxp = maxsf-minsf
        return maxp

Log in to reply
 

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