Simple Python O(n)


  • 0
    A
    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            if(len(prices) == 0):
                return 0
            smallest = prices[0]
            max_profit = 0
            for elem in prices:
                if(elem < smallest):
                    smallest = elem
                else:
                    max_profit = max(max_profit, elem-smallest)
            
            return(max_profit)
            
    

Log in to reply
 

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