Skip to main content

122. Best Time to Buy and Sell Stock II

mediumAsked at Robinhood

Same setup as Best Time I but with unlimited transactions. Robinhood asks this as the natural follow-up — the optimal collapses to a one-line sum that surprises candidates who reach for DP first.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Robinhood loops.

  • Glassdoor (2026-Q1)Robinhood phone-screen reports list this as the universal follow-up to LeetCode 121.
  • Reddit r/leetcode (2025-11)Robinhood new-grad trip report describes this as the pivot question after the single-transaction warm-up.

Problem

You are given an integer array prices where prices[i] is the price of a 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 at any time. However, you can buy then immediately sell on the same day. Find and return the maximum profit you can achieve.

Constraints

  • 1 <= prices.length <= 3 * 10^4
  • 0 <= prices[i] <= 10^4

Examples

Example 1

Input
prices = [7,1,5,3,6,4]
Output
7

Explanation: Buy day 2 sell day 3 (+4), buy day 4 sell day 5 (+3). Total = 7.

Example 2

Input
prices = [1,2,3,4,5]
Output
4

Explanation: Buy day 1 sell day 5 (+4) — same as transacting every day.

Example 3

Input
prices = [7,6,4,3,1]
Output
0

Explanation: No transactions; profit = 0.

Approaches

1. DP with hold/cash states

At each day track the best profit if you currently hold a stock vs hold cash. Transition: cash = max(cash, hold + price); hold = max(hold, cash - price).

Time
O(n)
Space
O(1)
function maxProfitDP(prices) {
  let hold = -Infinity, cash = 0;
  for (const p of prices) {
    const prevCash = cash;
    cash = Math.max(cash, hold + p);
    hold = Math.max(hold, prevCash - p);
  }
  return cash;
}

Tradeoff: Generalizes cleanly to LC 123 (k=2), LC 188 (k transactions), with-cooldown, with-fee. Use this when the interviewer wants to see you handle the family of problems.

2. Greedy daily delta (optimal one-liner)

Sum every positive prices[i] - prices[i-1]. Each positive day is a buy yesterday + sell today.

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: Cleanest possible code. The proof is short: any rising segment from a to b is equivalent in profit to the sum of consecutive daily deltas (telescoping sum). Falling days contribute nothing — skip them.

Robinhood-specific tips

Robinhood interviewers love this follow-up because the right answer is so much simpler than candidates expect after solving LC 121. Don't reach for DP first. State the insight out loud: 'unlimited transactions with same-day buy-sell means I capture every positive day-over-day delta.' Then write the one-liner. If the interviewer asks for the DP version anyway (to gauge generalization), be ready to write it too — it's the gateway to LC 123/188.

Common mistakes

  • Treating it like LC 121 and tracking a single running min — gives a wrong answer when there are multiple peaks.
  • Forgetting the same-day buy/sell rule — it's what enables the telescoping greedy proof.
  • Subtracting (prices[i] - prices[i-1]) unconditionally — you lose money on falling days; only add positive deltas.

Follow-up questions

An interviewer at Robinhood may pivot to one of these next:

  • Best Time to Buy and Sell Stock III (LC 123) — at most 2 transactions.
  • Best Time to Buy and Sell Stock IV (LC 188) — at most k transactions; classic DP.
  • Best Time to Buy and Sell Stock with Cooldown (LC 309).
  • Best Time to Buy and Sell Stock with Transaction Fee (LC 714).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the greedy daily-delta trick work?

If you bought on day a and sold on day b (b > a), profit = prices[b] - prices[a] = sum of (prices[i] - prices[i-1]) for i = a+1 to b. So summing every positive consecutive delta captures every profitable segment without explicitly tracking them.

When should I use the DP solution instead?

When the interviewer signals they want the general framework — for instance, before pivoting to LC 188 (at most k transactions) or the cooldown/fee variants. The state-machine DP generalizes; the greedy doesn't.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Best Time to Buy and Sell Stock II and other Robinhood interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →