Skip to content

Maximum Points You Can Obtain from Cards

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

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.

card_points = [1, 2, 3, 4, 5, 6, 1], k = 3   →  12   (take 1, 6, 5 from the right)
card_points = [9, 7, 7, 9, 7, 7, 9], k = 7   →  55   (take everything)

Constraints

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