Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 309 · State Machine / Stocks

Best Time to Buy and Sell Stock with Cooldown

LeetCode 309 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

dfs(i, can_buy): selling moves to i+2 — that skipped day is the cooldown.

మధ్యస్థంArrayDynamic Programming
LeetCode లో తెరవండి ↗

నేను Best Time to Buy and Sell Stock with Cooldown (LeetCode 309) ను ఇలా పరిష్కరించాను — State Machine / Stocks ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Each day you are in one of two modes: free to buy, or holding a share. In buy mode you either buy or wait; in hold mode you either sell or wait. Selling forces you to skip the next day, which is just a jump of two days in the recursion.

Approach

  1. 1

    State: (index, can_buy) → best profit from this day onward.

  2. 2

    If can_buy: max(−price + dfs(i+1, holding), dfs(i+1, can_buy)).

  3. 3

    If holding: max(+price + dfs(i+2, can_buy), dfs(i+1, holding)).

  4. 4

    Base case: past the last day the profit is 0.

  5. 5

    lru_cache makes the 2n states each solve once.

నేను ఎలా పరిష్కరించాను

State machine + memoPythonO(n) time · O(n) space
from functools import lru_cache
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        #two choices
        #buy, sell
        #buy/cool down
        #sell/cool down
        #if sell we need to wait for a day
        n = len(prices)
        @lru_cache(maxsize=None)
        def dfs(index,can_buy):
            if index>=n:
                return 0
            if can_buy:
                buy = -prices[index]+dfs(index+1,False)
                cooldown= dfs(index+1,True)
                return max(buy,cooldown)
            else:
                sell = prices[index]+dfs(index+2,True)
                cooldown= dfs(index+1,False)
                return max(sell,cooldown)
        return dfs(0,True)
స్టెప్-త్రూ లోడ్ అవుతోంది…

Best Time to Buy and Sell Stock with Cooldown — తరచూ అడిగే ప్రశ్నలు

Best Time to Buy and Sell Stock with Cooldown వెనుక ఉన్న intuition ఏమిటి?

Each day you are in one of two modes: free to buy, or holding a share. In buy mode you either buy or wait; in hold mode you either sell or wait. Selling forces you to skip the next day, which is just a jump of two days in the recursion.

Best Time to Buy and Sell Stock with Cooldown (LeetCode 309) ను ఎలా పరిష్కరించాలి?

1. State: (index, can_buy) → best profit from this day onward. 2. If can_buy: max(−price + dfs(i+1, holding), dfs(i+1, can_buy)). 3. If holding: max(+price + dfs(i+2, can_buy), dfs(i+1, holding)). 4. Base case: past the last day the profit is 0. 5. lru_cache makes the 2n states each solve once.

Best Time to Buy and Sell Stock with Cooldown యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(n). (Accepted approach: State machine + memo.)

Best Time to Buy and Sell Stock with Cooldown లో సాధారణ పొరపాట్లు ఏమిటి?

The cooldown applies after a sell, not after a buy. Index can jump past n by two — the base case must use ≥ n.