← DiffPush

Maximum Points You Can Obtain from Cards

Standard Bar Sliding Window · Medium Problems O(n) · O(1)

The Dispatch Queue Card Pickup

A dispatch console shows a row of task cards, each carrying a completion score. An operator must claim exactly k cards but may only take from the two ends of the row. Because the claimed cards always form one prefix glued to one suffix, the unclaimed middle is a single contiguous block — and minimizing that block's score maximizes the haul.

Input: An integer array cardPoints and an integer k, the number of cards to claim.

Output: Return the maximum total score obtainable by claiming k cards from the row's ends.

Constraints

Examples

Example 1

Input: {"cardPoints":[1,2,3,4,5,6,1],"k":3}
Output: 12
Claiming 1+6+5 from the ends beats any other split of three picks.

Example 2

Input: {"cardPoints":[2,2,2],"k":2}
Output: 4
All cards score alike, so any two picks yield the same total.

Solve this in your browser →

Also on LeetCode ↗