Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array prices where prices[i] is the price of a stock in dollars on the ith day, and an integer k.
You are allowed to make at most k transactions, where each transaction can be either of the following:
Normal transaction: Buy on day i, then sell on a later day j where i < j. You profit prices[j] - prices[i].
Short selling transaction: Sell on day i, then buy back on a later day j where i < j. You profit prices[i] - prices[j].
Note that you must complete each transaction before starting another. Additionally, you can't buy or sell on the same day you are selling or buying back as part of a previous transaction.
Return the maximum total profit you can earn by making at most k transactions.
Example 1:
Input: prices = [1,7,9,8,2], k = 2
Output: 14
Explanation:
We can make $14 of profit through 2 transactions:Example 2:
Input: prices = [12,16,19,19,8,1,19,13,9], k = 3
Output: 36
Explanation:
We can make $36 of profit through 3 transactions:Constraints:
2 <= prices.length <= 1031 <= prices[i] <= 1091 <= k <= prices.length / 2Problem summary: You are given an integer array prices where prices[i] is the price of a stock in dollars on the ith day, and an integer k. You are allowed to make at most k transactions, where each transaction can be either of the following: Normal transaction: Buy on day i, then sell on a later day j where i < j. You profit prices[j] - prices[i]. Short selling transaction: Sell on day i, then buy back on a later day j where i < j. You profit prices[i] - prices[j]. Note that you must complete each transaction before starting another. Additionally, you can't buy or sell on the same day you are selling or buying back as part of a previous transaction. Return the maximum total profit you can earn by making at most k transactions.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,7,9,8,2] 2
[12,16,19,19,8,1,19,13,9] 3
best-time-to-buy-and-sell-stock)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3573: Best Time to Buy and Sell Stock V
class Solution {
public long maximumProfit(int[] prices, int k) {
int n = prices.length;
long[][][] f = new long[n][k + 1][3];
for (int j = 1; j <= k; ++j) {
f[0][j][1] = -prices[0];
f[0][j][2] = prices[0];
}
for (int i = 1; i < n; ++i) {
for (int j = 1; j <= k; ++j) {
f[i][j][0] = Math.max(f[i - 1][j][0],
Math.max(f[i - 1][j][1] + prices[i], f[i - 1][j][2] - prices[i]));
f[i][j][1] = Math.max(f[i - 1][j][1], f[i - 1][j - 1][0] - prices[i]);
f[i][j][2] = Math.max(f[i - 1][j][2], f[i - 1][j - 1][0] + prices[i]);
}
}
return f[n - 1][k][0];
}
}
// Accepted solution for LeetCode #3573: Best Time to Buy and Sell Stock V
func maximumProfit(prices []int, k int) int64 {
n := len(prices)
f := make([][][3]int, n)
for i := range f {
f[i] = make([][3]int, k+1)
}
for j := 1; j <= k; j++ {
f[0][j][1] = -prices[0]
f[0][j][2] = prices[0]
}
for i := 1; i < n; i++ {
for j := 1; j <= k; j++ {
f[i][j][0] = max(f[i-1][j][0], f[i-1][j][1]+prices[i], f[i-1][j][2]-prices[i])
f[i][j][1] = max(f[i-1][j][1], f[i-1][j-1][0]-prices[i])
f[i][j][2] = max(f[i-1][j][2], f[i-1][j-1][0]+prices[i])
}
}
return int64(f[n-1][k][0])
}
# Accepted solution for LeetCode #3573: Best Time to Buy and Sell Stock V
class Solution:
def maximumProfit(self, prices: List[int], k: int) -> int:
n = len(prices)
f = [[[0] * 3 for _ in range(k + 1)] for _ in range(n)]
for j in range(1, k + 1):
f[0][j][1] = -prices[0]
f[0][j][2] = prices[0]
for i in range(1, n):
for j in range(1, k + 1):
f[i][j][0] = max(
f[i - 1][j][0],
f[i - 1][j][1] + prices[i],
f[i - 1][j][2] - prices[i],
)
f[i][j][1] = max(f[i - 1][j][1], f[i - 1][j - 1][0] - prices[i])
f[i][j][2] = max(f[i - 1][j][2], f[i - 1][j - 1][0] + prices[i])
return f[n - 1][k][0]
// Accepted solution for LeetCode #3573: Best Time to Buy and Sell Stock V
impl Solution {
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
let n = prices.len();
let k = k as usize;
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
for j in 1..=k {
f[0][j][1] = -(prices[0] as i64);
f[0][j][2] = prices[0] as i64;
}
for i in 1..n {
for j in 1..=k {
f[i][j][0] = f[i - 1][j][0]
.max(f[i - 1][j][1] + prices[i] as i64)
.max(f[i - 1][j][2] - prices[i] as i64);
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
}
}
f[n - 1][k][0]
}
}
// Accepted solution for LeetCode #3573: Best Time to Buy and Sell Stock V
function maximumProfit(prices: number[], k: number): number {
const n = prices.length;
const f: number[][][] = Array.from({ length: n }, () =>
Array.from({ length: k + 1 }, () => Array(3).fill(0)),
);
for (let j = 1; j <= k; ++j) {
f[0][j][1] = -prices[0];
f[0][j][2] = prices[0];
}
for (let i = 1; i < n; ++i) {
for (let j = 1; j <= k; ++j) {
f[i][j][0] = Math.max(
f[i - 1][j][0],
f[i - 1][j][1] + prices[i],
f[i - 1][j][2] - prices[i],
);
f[i][j][1] = Math.max(f[i - 1][j][1], f[i - 1][j - 1][0] - prices[i]);
f[i][j][2] = Math.max(f[i - 1][j][2], f[i - 1][j - 1][0] + prices[i]);
}
}
return f[n - 1][k][0];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.