Участник:Nik7/Best Time to Buy and Sell Stock III

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

«Best Time to Buy and Sell Stock III»

import math
 
class Solution:
    def maxProfit(self, prices: List[int]) -> int:        
        first_buy = -math.inf
        first_sell = -math.inf
        second_buy = -math.inf
        second_sell = -math.inf
        for price in prices:
            first_buy = max(first_buy, -price)
            first_sell = max(first_sell, first_buy + price)
            second_buy = max(second_buy, first_sell - price)
            second_sell = max(second_sell, second_buy + price)
        return second_sell