Data Structures & AlgorithmsHardCodeforces Div2 C · LeetCode-style

Cards with values [4, 1, 7, 3, 9, 2, 5] lie in a row. In each of 3 moves you take the card at the LEFT end or the RIGHT end. What is the maximum total value you can collect?

  1. A. 16
  2. B. 11
  3. C. 18
  4. D. 21

Correct answer: A. 16

A prefix plus a suffix of total length k: try every split of k into left and right takes, using prefix and suffix sums. Greedily taking the larger visible end at each move can give up a better combination further in.

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

Exams that ask this topic