Whether you’re preparing for tech interviews or want to dominate stock-based dynamic programming questions, this guide covers all major variants of buy-sell stock problems, explains the core logic, and equips you with reusable templates and decision strategies.
🔥 Categories of Stock Problems
Leetcode | Variant | Max Transactions | Special Rules |
---|---|---|---|
121 | Stock I | 1 | Simple |
122 | Stock II | ∞ | Multiple buys/sells |
123 | Stock III | 2 | Limited transactions |
188 | Stock IV | k | Generalized |
309 | Stock with Cooldown | ∞ | 1-day cooldown |
714 | Stock with Transaction Fee | ∞ | Fee on each sell |
🧩 Core Concepts & Patterns
1. ✅ Greedy – When transactions are unlimited
Problem: Leetcode 122
If you’re allowed to buy and sell as many times as you want, just collect every profityou see.
function maxProfit(prices: number[]): number {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
2. ✅ Tracking Min/Max Price – 1 Transaction
Problem: Leetcode 121
Find the minimum price so far, and compute profit as prices[i] - minPrice
.
function maxProfit(prices: number[]): number {
let minPrice = prices[0];
let profit = 0;
for (let i = 1; i < prices.length; i++) {
profit = Math.max(profit, prices[i] - minPrice);
minPrice = Math.min(minPrice, prices[i]);
}
return profit;
}
3. ✅ Two Transactions – 2-pass or 4-variable
Problem: Leetcode 123
Approach 1: Two Pass DP
- First pass:
leftProfit[i] = max profit from day 0 to i
- Second pass:
rightProfit[i] = max profit from day i to end
Final result = max(left[i] + right[i])
Approach 2: 4 Variable Trick
let buy1 = -Infinity, sell1 = 0;
let buy2 = -Infinity, sell2 = 0;
for (let price of prices) {
buy1 = Math.max(buy1, -price);
sell1 = Math.max(sell1, buy1 + price);
buy2 = Math.max(buy2, sell1 - price);
sell2 = Math.max(sell2, buy2 + price);
}
return sell2;
4. ✅ DP for k Transactions – Generalized
Problem: Leetcode 188
function maxProfit(k: number, prices: number[]): number {
const n = prices.length;
if (k >= n / 2) return unlimitedProfit(prices);
let prev = new Array(n).fill(0);
let curr = new Array(n).fill(0);
for (let t = 1; t <= k; t++) {
let maxDiff = -prices[0];
for (let i = 1; i < n; i++) {
curr[i] = Math.max(curr[i - 1], prices[i] + maxDiff);
maxDiff = Math.max(maxDiff, prev[i] - prices[i]);
}
[prev, curr] = [curr, prev];
}
return prev[n - 1];
}
5. ✅ Cooldown Day Between Transactions
Problem: Leetcode 309
Track 3 states:
hold
→ you’re holding a stocksold
→ just soldrest
→ not holding, not sold today
let hold = -prices[0], sold = 0, rest = 0;
for (let i = 1; i < prices.length; i++) {
let prevSold = sold;
sold = hold + prices[i];
hold = Math.max(hold, rest - prices[i]);
rest = Math.max(rest, prevSold);
}
return Math.max(sold, rest);
6. ✅ Transaction Fee
Problem: Leetcode 714
Same as greedy but subtract fee at sell time.
let hold = -prices[0], cash = 0;
for (let i = 1; i < prices.length; i++) {
hold = Math.max(hold, cash - prices[i]);
cash = Math.max(cash, hold + prices[i] - fee);
}
return cash;
🧠 How to Solve Any Stock Problem in Future?
✅ Ask Yourself:
-
How many transactions?
- 1 → use minPrice pattern
- 2 → use 2-pass or 4 variables
- k → use DP with transaction loops
-
Is there a cooldown or fee?
- Cooldown → use 3 state DP (hold, sold, rest)
- Fee → subtract on sell
-
Unlimited transactions?
- Greedy solution
-
Need to optimize space?
- Use rolling arrays or 2 arrays:
prev
,curr
- Use rolling arrays or 2 arrays:
🏁 TL;DR – Your Stock Problem Toolkit
Variant | Use Pattern |
---|---|
Stock I (121) | Track minPrice + maxProfit |
Stock II (122) | Greedy |
Stock III (123) | 2-pass DP or 4 variables |
Stock IV (188) | DP with rolling arrays |
Cooldown (309) | 3-state DP (hold, sold, rest) |
Fee (714) | Modify greedy + fee logic |
📦 Bonus: Practice List
- Leetcode 121 – Buy/Sell I
- Leetcode 122 – Buy/Sell II
- Leetcode 123 – Buy/Sell III
- Leetcode 188 – Buy/Sell IV
- Leetcode 309 – Buy/Sell w/ Cooldown
- Leetcode 714 – Buy/Sell w/ Fee
🎓 Final Words
Learning to recognize patterns is the secret to cracking any stock DP problem.
Don’t memorize solutions — instead, understand the structure and variations. Once you grasp that, you’ll be able to solve any stock problem they throw at you in interviews 🚀.