Data Structures & AlgorithmsHard

Why does the two-transaction stock problem split the array at a point and solve each side independently?

  1. A. Two transactions can always be merged into one.
  2. B. With at most two transactions, the two buy-sell pairs can be separated by a single cut, so the best answer is the best split of the one-transaction profits.
  3. C. The optimal two transactions are always the first and last days.
  4. D. Splitting is unnecessary because each transaction is independent of the other.

Correct answer: B. With at most two transactions, the two buy-sell pairs can be separated by a single cut, so the best answer is the best split of the one-transaction profits.

Any two non-overlapping transactions can be separated by a cut day, so the answer is the maximum over cuts of the best single-transaction profit on each side. A running minimum computes each side in linear time.

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

Exams that ask this topic