Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 C

Daily prices are [3, 2, 6, 5, 0, 3, 8, 1, 9]. You may complete at most 3 buy-then-sell transactions (one share at a time, sell before the next buy). What is the maximum profit?

  1. A. 16
  2. B. 24
  3. C. 17
  4. D. 20

Correct answer: D. 20

DP with two states per transaction count: buy[t] = max(buy[t], sell[t−1] − price) and sell[t] = max(sell[t], buy[t] + price). The number of transactions is a hard limit; a version without it (unlimited trades) gives a larger profit.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic