73. Best Time to Buy and Sell Stock II
mediumAsked at VercelBuy and sell as many times as you want; find the max profit. Vercel asks this for the elegant greedy 'sum every positive gain' insight — same intuition as their adaptive bandwidth optimizer that captures every favorable swing.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel screen; one-liner greedy is the bonus.
- Blind (2026-Q1)— Listed in Vercel platform engineer screen.
Problem
You are given an integer array prices where prices[i] is the price of a given stock on the i-th 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 buy it then immediately sell it on the same day. Find and return the maximum profit you can achieve.
Constraints
1 <= prices.length <= 3 * 10^40 <= prices[i] <= 10^4
Examples
Example 1
prices = [7,1,5,3,6,4]7Explanation: Buy on day 2 (price 1), sell on day 3 (5). Buy on day 4 (3), sell on day 5 (6).
Example 2
prices = [1,2,3,4,5]4Example 3
prices = [7,6,4,3,1]0Approaches
1. DP with state (have stock / no stock)
dp[i][0] = max profit on day i with no stock; dp[i][1] = with stock. Transitions: no = max(no_prev, have_prev + price); have = max(have_prev, no_prev - price).
- Time
- O(n)
- Space
- O(n)
function maxProfit(prices) {
let no = 0, have = -Infinity;
for (const p of prices) {
no = Math.max(no, have + p);
have = Math.max(have, no - p);
}
return no;
}Tradeoff: Generalizes to LC 309/714 with extra constraints. For unlimited transactions, the greedy is simpler.
2. Greedy: sum every positive diff (optimal)
For each consecutive pair (i, i+1), add max(0, prices[i+1] - prices[i]).
- Time
- O(n)
- Space
- O(1)
function maxProfit(prices) {
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;
}Tradeoff: Two-line greedy. The intuition: any multi-day rising streak can be decomposed into consecutive 1-day gains, all of which are positive. Summing positives = total profit.
Vercel-specific tips
Vercel grades the greedy and its justification. Bonus signal: proving the equivalence — any optimal strategy's profit equals the sum of positive consecutive diffs (because longer holds telescope into a sequence of 1-day diffs). Mention the DP as the generalization for harder variants.
Common mistakes
- Trying LC 121's single-buy-single-sell — wrong for this problem.
- Adding the diff even when negative — produces wrong (negative) profit.
- Holding for the 'maximum span' — same answer as greedy but harder to code right.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Buy and sell with cooldown (LC 309).
- Buy and sell with transaction fee (LC 714).
- At most k transactions (LC 188).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does summing positive diffs equal the optimum?
Any 'buy at low, sell at high' span can be telescoped: prices[r] - prices[l] = sum of (prices[i+1] - prices[i]) for i in [l, r-1]. If we only sum positive diffs, we capture every favorable step and avoid every unfavorable one — strictly best.
Why doesn't this overcount?
Because we 'sell and rebuy on the same day' is free under the problem rules. So pretending we trade every favorable day is equivalent to any optimal multi-day strategy.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock II and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →