2Brute2Best
🔥0

Best Time to Buy and Sell Stock with Cooldown

Medium·LeetCode #309 Pro OnlySlight Variation
LeetCode
01

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.

Input

An integer array prices.

Output

The largest total profit, following the cooldown rule.

Example 1
Input: prices = [1, 2, 3, 0, 2]
Output: 3

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.

Example 2
Input: prices = [1]
Output: 0

There is only one day, so no trade is possible.

Constraints
  • • 1 ≤ prices.length ≤ 5000
  • • 0 ≤ prices[i] ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“cannot buy on the next day”
→ A sale creates a cooldown day. The day after a sale is a different state from a normal free day.
“as many transactions as you like”
→ Many trades, so the state of each day is what we keep, not the list of trades.
“hold at most one share”
→ Holding is one state with one purchase price in it.
03

Approach Complexity Ladder

Brute Force — recursion with a jump after a saleO(2^N)O(N)

On each day, try buy, keep, or sell; a sale jumps two days ahead

Better — memoized recursion (day, holding)O(N)O(N)

Cache each (day, holding) pair so it is solved only once

Optimal — three running valuesO(N)O(1)

Track holding, just sold, and free to buy; update all three each day

Brute Force Intuition

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:

Walk the days carrying the best value for each state (holding / not holding), or the best choice so far from each side.
Remember this · DP — Kadane / Subarray
Trigger
Maximum (or minimum) sum/product of a contiguous subarray, stock buy/sell, circular arrays.
Think
Is the best subarray ending at i either "i alone" or "best ending at i−1, plus i"?
Key question
Should I extend or restart?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Kadane / Subarray translate into production architectures and low-level memory mechanics.