Imagine you're playing a strategic card game where you have a row of cards laid out in front of you, each with a specific point value. The twist? You can only pick cards from the very beginning or the very end of the row - no cherry-picking from the middle!
You're given an array cardPoints representing the points on each card, and you must take exactly k cards to maximize your score. Your goal is to determine the maximum possible sum of points you can achieve.
Example: If you have cards [1,2,3,4,5,6,1] and need to pick k=3 cards, you could take the first 3 cards (1+2+3=6), the last 3 cards (6+1+1=8), or a mix like first 1 and last 2 cards (1+1+1=3). The optimal strategy gives you 8 points!
Input: An integer array cardPoints and an integer k
Output: The maximum score (sum) you can obtain by picking exactly k cards from either end
Input & Output
Constraints
- 1 ≤ cardPoints.length ≤ 105
- 1 ≤ cardPoints[i] ≤ 104
- 1 ≤ k ≤ cardPoints.length
- You must take exactly k cards
- Cards can only be taken from the beginning or end of the array