Data Structures & AlgorithmsVery hardCodeforces Div2 C · CAT · MBA placement

Stock prices by day are [3, 3, 5, 0, 0, 3, 1, 4, 9, 2]. You may complete at most TWO buy-then-sell transactions, and you must sell before buying again. What is the maximum profit?

  1. A. 9
  2. B. 13
  3. C. 14
  4. D. 11

Correct answer: D. 11

Use four running states: after the first buy, after the first sell, after the second buy, after the second sell. Allowing any number of trades (summing every rise) overshoots the limit of two.

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

Exams that ask this topic