Normal logic, Python Code


  • 0
    R
    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            size = len(prices)
            buy, sell, i, profit = 0, 0, 0, 0
            while i < len(prices):
                if prices[i] > prices[sell]:
                    sell = i
                if prices[i] < prices[sell]:
                    if i > sell:
                        profit += prices[sell] - prices[buy]
                    buy = sell = i
                i += 1
           
            if sell > buy:
                profit += prices[sell] - prices[buy]
            
            return profit
    

Log in to reply
 

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