Maximum Points You Can Obtain from Cards
You are given an integer array cardPoints and an integer k. In one move, you may take one card from either the beginning or the end of the row. Return the…
You are given an integer array `cardPoints` and an integer `k`. In one move, you may take one card from either the beginning or the end of the row. Return the maximum score obtainable after taking exactly `k` cards.
- `1 <= cardPoints.length <= 10^5` - `1 <= cardPoints[i] <= 10^4` - `1 <= k <= cardPoints.length` - The interactive visualizer accepts at most 12 cards; longer valid arrays are execution-only.
Example 1: input cardPoints = [1,2,3,4,5,6,1], k = 3; output 12. Taking the final three cards yields 1 + 6 + 5 = 12.
Example 2: input cardPoints = [2,2,2], k = 2; output 4. Any two cards have a total score of four.
Category: Sliding Window. Topic: sliding-window. Difficulty: Medium. Patterns: sliding-window, prefix-sum.