dsa · medium
Maximum Points You Can Obtain from Cards
You are given card_points, an array of integers, and an integer k. You must take exactly k cards. On each turn you may take a card from either the very start or the very end of the row. Maximize the total points of the cards you take and return that total.
Arguments
card_points— the points on each card, left to rightk— the exact number of cards to take
Example
card_points = [1,2,3,4,5,6,1], k = 3
Taking the last three cards (6, 1) plus one more (5) gives 12 — the best possible → 12.
card_points = [2,2,2], k = 2 → any two cards give 4 → 4.
Constraints
1 <= card_points.length <= 10^5 1 <= card_points[i] <= 10^4 1 <= k <= card_points.length Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,2,3,4,5,6,1] 3 Expected: 12
Example 2
Input: [2,2,2] 2 Expected: 4
Example 3
Input: [9,7,7,9,7,7,9] 7 Expected: 55