Possible similar problem, eg after buying a stock, you must hold it one day before youn sell it. DP Python


  • 0
    W
    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            n=len(prices)
            if n<=1:
                return 0
            ws,wos=[0 for i in range(n)],[0 for i in range(n)]
            buyit=sellit=0
            for i in range(n):
                if i<=2:
                    buyit=-prices[i]
                else:
                    buyit=wos[i-2]-prices[i]
                if i==0:
                    ws[i]=buyit
                else:
                    ws[i]= max(ws[i-1],buyit)
                    sellit=ws[i-1]+prices[i]
                    wos[i]=max(wos[i-1],sellit) 
            return wos[n-1]

Log in to reply
 

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