Concise python answer

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

    if the next day's price is bigger than today's, just buy it today and sell it tomorrow, you can get all the profit.

Log in to reply

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