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 given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can sell and buy the stock multiple times on the same day, ensuring you never hold more than one share of the stock.
Find and return the maximum profit you can achieve.
Example 1:
Input: prices = [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.
Example 2:
Input: prices = [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.
Example 3:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
Constraints:
1 <= prices.length <= 3 * 1040 <= prices[i] <= 104Problem summary: You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can sell and buy the stock multiple times on the same day, ensuring you never hold more than one share of the stock. Find and return the maximum profit you can achieve.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Greedy
[7,1,5,3,6,4]
[1,2,3,4,5]
[7,6,4,3,1]
best-time-to-buy-and-sell-stock)best-time-to-buy-and-sell-stock-iii)best-time-to-buy-and-sell-stock-iv)best-time-to-buy-and-sell-stock-with-cooldown)best-time-to-buy-and-sell-stock-with-transaction-fee)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #122: Best Time to Buy and Sell Stock II
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for (int i = 1; i < prices.length; ++i) {
ans += Math.max(0, prices[i] - prices[i - 1]);
}
return ans;
}
}
// Accepted solution for LeetCode #122: Best Time to Buy and Sell Stock II
func maxProfit(prices []int) (ans int) {
for i, v := range prices[1:] {
t := v - prices[i]
if t > 0 {
ans += t
}
}
return
}
# Accepted solution for LeetCode #122: Best Time to Buy and Sell Stock II
class Solution:
def maxProfit(self, prices: List[int]) -> int:
return sum(max(0, b - a) for a, b in pairwise(prices))
// Accepted solution for LeetCode #122: Best Time to Buy and Sell Stock II
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut res = 0;
for i in 1..prices.len() {
res += (0).max(prices[i] - prices[i - 1]);
}
res
}
}
// Accepted solution for LeetCode #122: Best Time to Buy and Sell Stock II
function maxProfit(prices: number[]): number {
let ans = 0;
for (let i = 1; i < prices.length; i++) {
ans += Math.max(0, prices[i] - prices[i - 1]);
}
return ans;
}
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.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.