Участник:Easik/best-time-to-buy-and-sell-stock-with-cooldown

Материал из DISCOPAL
Перейти к: навигация, поиск

Python

import numpy as np
 
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
 
        days = len(prices)
        if days <= 1:
            return 0
 
        s0 = days * [0]
        s1 = days * [0]
        s2 = days * [0]
 
        s0[0] = 0
        s1[0] = -prices[0]
        s2[0] = -np.inf
 
        for i in range(1, days):
            s0[i] = max(s0[i - 1], s2[i - 1])
            s1[i] = max(s1[i - 1], s0[i - 1] - prices[i])
            s2[i] = s1[i - 1] + prices[i]
 
        max_profit = max(s0[days - 1], s2[days - 1])
 
        return max_profit