10. Stock Buy and Sell with Cooldown
β GFG solution to the Stock Buy and Sell with Cooldown problem: maximize profit from stock trading with mandatory cooldown period using dynamic programming state machine approach. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [0, 2, 1, 2, 3]
Output: 3
Explanation: You first buy on day 1, sell on day 2 then cool down, then buy on day 4, and sell on day 5.
The total profit earned is (2-0) + (3-2) = 3, which is the maximum achievable profit.Example 2
Input: arr[] = [3, 1, 6, 1, 2, 4]
Output: 7
Explanation: You first buy on day 2 and sell on day 3 then cool down, then again you buy on day 5
and then sell on day 6. Clearly, the total profit earned is (6-1) + (4-2) = 7, which is the maximum achievable profit.π Constraints
β
My Approach
State Machine DP with Constant Space
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated