Simple Python solution, time: O(n) Space:O(1)


  • 0
    Y
    def maxProfit(self, prices):
            ans = 0
            minimum = 2**32
            for i in range(0,len(prices)-1):
                minimum = min(minimum, prices[i])
                ans =  max(ans,prices[i+1] - minimum)
            return ans
    

Log in to reply
 

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