Simple Python, beats 96% of submissions.

  • 0
    class Solution(object):
        def maxProfit(self, prices):
            :type prices: List[int]
            :rtype: int
            profit, buyDay = 0, 0
            for i in xrange(len(prices)):
                if (prices[i] - prices[buyDay]) > profit: 
                    profit = prices[i] - prices[buyDay]
                elif prices[i] < prices[buyDay]: buyDay = i
            return profit

Log in to reply

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