Best Time to Buy and Sell Stock III

Solution Code
class Solution:def maxProfit(self, prices: list[int]) -> int:if not prices:return 0buy1, buy2 = float('-inf'), float('-inf')
    sell1, sell2 = 0, 0
    
    for price in prices:
        buy1 = max(buy1, -price)
        sell1 = max(sell1, buy1 + price)
        buy2 = max(buy2, sell1 - price)
        sell2 = max(sell2, buy2 + price)
        
    return sell2

Time Complexity

O(N)

Space Complexity

O(1)