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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example 1:
Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 1050 <= prices[i] <= 104Problem summary: You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[7,1,5,3,6,4]
[7,6,4,3,1]
maximum-subarray)best-time-to-buy-and-sell-stock-ii)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)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #121: Best Time to Buy and Sell Stock
class Solution {
public int maxProfit(int[] prices) {
int ans = 0, mi = prices[0];
for (int v : prices) {
ans = Math.max(ans, v - mi);
mi = Math.min(mi, v);
}
return ans;
}
}
// Accepted solution for LeetCode #121: Best Time to Buy and Sell Stock
func maxProfit(prices []int) (ans int) {
mi := prices[0]
for _, v := range prices {
ans = max(ans, v-mi)
mi = min(mi, v)
}
return
}
# Accepted solution for LeetCode #121: Best Time to Buy and Sell Stock
class Solution:
def maxProfit(self, prices: List[int]) -> int:
ans, mi = 0, inf
for v in prices:
ans = max(ans, v - mi)
mi = min(mi, v)
return ans
// Accepted solution for LeetCode #121: Best Time to Buy and Sell Stock
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut ans = 0;
let mut mi = prices[0];
for &v in &prices {
ans = ans.max(v - mi);
mi = mi.min(v);
}
ans
}
}
// Accepted solution for LeetCode #121: Best Time to Buy and Sell Stock
function maxProfit(prices: number[]): number {
let ans = 0;
let mi = prices[0];
for (const v of prices) {
ans = Math.max(ans, v - mi);
mi = Math.min(mi, v);
}
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.