Help with Memory Limit Exceeded


  • 0
    J

    Is the problem of "column()" function?
    Thanks!

    def column(matrix,i):
        return [row[i] for row in matrix]
        
    class Solution:
        # @param prices, a list of integer
        # @return an integer
        def maxProfit(self, prices):
            if len(prices) <= 1:
                return 0
            
            profits=[[0 for i in range(len(prices))] for j in range(len(prices))]
            for j in range(len(prices)):
                for i in range(j):
                    profits[j][i] = prices[i] - prices[j]
            
            # one transaction
            maxprofit=[max(max(profits))]
            
            # two transaction one before day k, one after day k
            #profit1=max(max(tocolumn(profits[:k],k)))
            profit1=[0]
            for i in range(1,len(prices)):            #sell day
                if max(profits[i]) > profit1[-1]:
                    profit1.append(max(profits[i]))
                else:
                    profit1.append(profit1[-1])
                    
            #profit2=max(max(fromcolumn(profits[k:],k)))
            profit2=[0]
            for i in range(len(prices)-2,-1,-1):
                if max(column(profits,i)) > profit2[-1]:
                    profit2.append(max(column(profits,i)))
                else:
                    profit2.append(profit2[-1])
            profit2.reverse()
            
            for i in range(len(profit1)):
                maxprofit.append(profit1[i]+profit2[i])
    
            return max(maxprofit)

Log in to reply
 

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