Skip to content
Decoder

Maximum Points You Can Obtain from Cards

Medium

Problem

Cards are laid out in a row with values card_points. You take exactly k cards, one at a time, and each time you may take either the leftmost or the rightmost remaining card. Return the maximum total you can collect.

Example 1

Input
card_points = [1, 2, 3, 4, 5, 6, 1], k = 3
Output
12
Why
take 1, 6, 5 from the right

Example 2

Input
card_points = [9, 7, 7, 9, 7, 7, 9], k = 7
Output
55
Why
take everything

Constraints

  • 1 <= card_points.length <= 10^5
  • 1 <= card_points[i] <= 10^4
  • 1 <= k <= card_points.length

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.