Simple Python Solution


  • 0
    R

    Self explanatory

        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            if len(prices) < 1 or len(prices) == 1:
                return 0
            min_element = prices[0]
            diff = prices[1] - min_element
            for i in range(1, len(prices)):
                diff = max(diff, prices[i] - min_element)
                min_element = min(min_element, prices[i])
            return max(0, diff)
           
    

Log in to reply
 

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