2Brute2Best
🔥0

Best Time to Buy and Sell Stock II

Medium·LeetCode #122 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given prices, where prices[i] is the price of one share on day i. On each day you may buy one share, sell the share you hold, or do nothing.

You can hold at most one share at any time. You may trade as many times as you like. You may also sell and buy again on the same day, as long as you never hold two shares.

Return the largest profit you can make.

Input

An integer array prices.

Output

The largest total profit from any number of buys and sells.

Example 1
Input: prices = [7, 1, 5, 3, 6, 4]
Output: 7

Buy at 1 (index 1) and sell at 5 (index 2): profit 4. Then buy at 3 (index 3) and sell at 6 (index 4): profit 3. The total is 4 + 3 = 7.

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

Buy at 1 and sell at 5 for 4. Trading every day gives the same total, because 1 + 1 + 1 + 1 = 4.

Example 3
Input: prices = [7, 6, 4, 3, 1]
Output: 0

The price only falls, so the best choice is to never buy.

Constraints
  • • 1 ≤ prices.length ≤ 3 × 10^4
  • • 0 ≤ prices[i] ≤ 10^4
02

Pattern Recognition Clues

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

“buy and sell as many times as you like”
→ Many trades: the best path is no longer one buy and one sell, so track the state of each day instead of the trades.
“hold at most one share”
→ There are only two states per day: holding one share, or not holding.
“largest profit”
→ The answer is the best of the two states on the last day, and each state updates with O(1) work per day.
03

Approach Complexity Ladder

Brute Force — recursion over (day, holding)O(2^N)O(N)

On each day, try every allowed action; no memory

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

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

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

Keep the best cash and best hold values; update both once per day

Brute Force Intuition

Ask one question per day: am I holding a share or not? From the answer, try each allowed action (buy, sell, or nothing) and let the recursion handle the rest of the days. Keep the best total.

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.