Best Time to Buy and Sell Stock with Cooldown
Problem Statement
You are given prices, where prices[i] is the price of one share on day i. You may buy and sell as many times as you like, but you hold at most one share at a time.
There is one rule: after you sell, you cannot buy on the next day. That next day is a cooldown day.
Return the largest profit you can make.
An integer array prices.
The largest total profit, following the cooldown rule.
Buy at 1 (day 0), sell at 2 (day 1). Day 2 is the cooldown. Buy at 0 (day 3), sell at 2 (day 4). The profit is (2 − 1) + (2 − 0) = 3.
There is only one day, so no trade is possible.
- • 1 ≤ prices.length ≤ 5000
- • 0 ≤ prices[i] ≤ 1000
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
On each day, try buy, keep, or sell; a sale jumps two days ahead
Cache each (day, holding) pair so it is solved only once
Track holding, just sold, and free to buy; update all three each day
Recursion on the day and whether we hold a share. Buying or skipping moves to the next day. Selling moves to two days later, because the next day is the cooldown. Keep the best path.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Kadane / Subarray translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.