gary@interview:~/interview/coding/123-best-time-to….md$
$ cat ./coding/123-best-time-to-buy-and-sell-stock-iii.md
[Coding]

123. Best Time to Buy and Sell Stock III

────────────────────────────────────────────────────────────

123. Best Time to Buy and Sell Stock III

Final Profit = (Initial Profit — Buying Price) + Selling Price

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        firstBuy, firstSell = float('-inf'), 0
        secondBuy, secondSell = float('-inf'), 0

        for price in prices:
            firstBuy = max(firstBuy, -price)
            firstSell = max(firstSell, firstBuy + price)
            secondBuy = max(secondBuy, firstSell - price)
            secondSell = max(secondSell, secondBuy + price)

        return secondSell

--tags#Classic
$ ls ./coding/ | grep -v 123-best-time-to-buy-and-sell-stock-iii
265. Paint House II256. Paint House143. Reorder List1762. Buildings With an Ocean View
← cd ../codingcd ~