Maximum Points You Can Obtain from Cards
Medium · sliding window
A row of cards lies face up on a table. The list card_points gives the points printed on each card, from left to right.
You take exactly k cards, one per turn. On each turn you must take either the leftmost or the rightmost card still in the row. The cards you take add to your score.
Return the largest score you can finish with.
Examples
Example 1
Input: card_points = [3, 1, 1, 9, 2, 6, 5], k = 3
Output: 14
Take the 3 from the left end, then the 6 and the 5 from the right end.
Example 2
Input: card_points = [10, 1, 1, 1, 10], k = 2
Output: 20
Take a 10 from each end.
Example 3
Input: card_points = [4, 1, 8, 3], k = 4
Output: 16
You must take every card, so the score is their total.
Constraints
- 1 <= len(card_points) <= 10^5
- 1 <= card_points[i] <= 10^4
- 1 <= k <= len(card_points)