My stupid python solution


  • 0
    J
    class Solution(object):
    def maxProfit(self, prices):
        length = len(prices)
        if length<2:
        	return 0
        buy = None
        res = [0 for i in range(length)]
        for i in range(1,length):
        	if prices[i]<=prices[i-1]:
        		res[i] = res[i-1]
        	else:
        		if i == 1:
        			res[i] = prices[i]-prices[i-1]
        			buy = i-1
        		else:
        			if prices[i-1]>prices[i-2]:
        				res[i] = res[i-1]+prices[i]-prices[i-1]
        				if res[i-1] == res[i-2]:
        					buy = i-1
        			else:
        				submax1 = prices[i]-prices[i-1]+(0 if i<3 else res[i-3])
        				if buy!=None:
        					submax2 = (res[buy-2] if buy>2 else 0) + prices[i]-prices[buy]
        				else:
        					submax2 = 0
        				res[i] = max(res[i-2],submax1,submax2)
        				if submax1 == res[i]:
        					buy = i-1
        					print buy
        return res[-1]

Log in to reply
 

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