LeetCode #1423 Medium

Maximum Points You Can Obtain from Cards

Maximum Points You Can Obtain from Cards is LeetCode 1423 (Medium). Cards lie in a row and cardPoints[i] is the score of card i. You take exactly k cards, one at a time, and each must come from the left end or the right end of the cards still on the table. Return the largest total you can collect.

  • Every card is worth at least 1 point.
  • There can be 10⁵ cards, so trying every sequence of left and right picks is far too slow.
Constraints
  • 1 <= cardPoints.length <= 10⁵
  • 1 <= cardPoints[i] <= 10⁴
  • 1 <= k <= cardPoints.length
sliding-windowarrayprefix-sum
Open on LeetCode ↗
02

Intuition

Cards only ever leave from the two ends, so the cards left on the table always form one unbroken block in the middle. That makes every legal hand some i cards from the left plus k − i cards from the right, and the order of picks does not matter.

With i ranging from k down to 0, there are only k + 1 hands to compare. Moving from one hand to the next is a single trade: give back the innermost left card and take the next card from the right end. Each trade changes the total by one subtraction and one addition.

How to spot this pattern

Picking only from the two ends of an array, with the order of picks irrelevant, means the choice is a prefix plus a suffix. That is a sliding window over a wrap-around, or equally a window of untaken cards in the middle.

03

Approach

Try it first

Before reading on, convince yourself that the untaken cards always form one contiguous block. Then list all hands for k = 3 as left-and-right splits, and find how to get from one hand's total to the next in O(1). Aim for O(k) time and O(1) space.

1

Take all k cards from the left

Set total to the sum of cardPoints[0] to cardPoints[k − 1], and let best start there. This is the hand with k left cards and no right cards, the first of the k + 1 hands, and it is legal, so the answer is at least this large.

2

Trade one left card for one right card

For i from 1 to k, subtract cardPoints[k − i] and add cardPoints[n − i]. The left run gives up its innermost card, so it still starts at index 0, and the right run grows inward from the last card. After trade i the hand is k − i left cards plus i right cards.

3

Keep the best hand

After each trade set best to the larger of best and total. After trade k every card comes from the right, so all k + 1 hands have been compared, and best is the answer.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxScore(self, cardPoints: list[int], k: int) -> int:
▶3 n = len(cardPoints)
▶4 total = sum(cardPoints[:k])
▶5 best = total
▶6 for i in range(1, k + 1):
▶7 total += cardPoints[n - i] - cardPoints[k - i]
▶8 best = max(best, total)
▶9 return best
10213243546516left run6+right run0=total6every hand3 left0 right2 left1 right1 left2 right0 left3 righttake all 3 from the left → total 6
k3
total6cards 0 to 2
Start with one legal hand: the first 3 cards, lifted out of the row. Their sum is 6. Whatever is picked, the cards left on the table form one block, so every hand is a run from the left end plus a run from the right end.
10213243546516left run6+right run0=total6every hand63 left0 right2 left1 right1 left2 right0 left3 rightbest starts at 6
best6
This hand, 3 left and 0 right, is the first of the 4 possible hands. Its total 6 is the best so far.
102132back43546516takeleft run3+right run1=total4every hand63 left0 right2 left1 right1 left2 right0 left3 rightgive back 3, take 1 → total 4
i1
give back3card 2
take1card 6
total4
Trade the innermost left card, card 2 worth 3, for the next card from the right end, card 6 worth 1. The left part still starts at card 0, so the hand stays legal: 2 left and 1 right. 6 − 3 + 1 = 4.
10213243546516left run3+right run1=total4every hand63 left0 right42 left1 right1 left2 right0 left3 right4 is not above 6 → best stays
best6
The hand with 2 left and 1 right scores 4, not more than the best of 6, so the best stays.
1021back32435465take16left run1+right run7=total8every hand63 left0 right42 left1 right1 left2 right0 left3 rightgive back 2, take 6 → total 8
i2
give back2card 1
take6card 5
total8
Trade the innermost left card, card 1 worth 2, for the next card from the right end, card 5 worth 6. The left part still starts at card 0, so the hand stays legal: 1 left and 2 right. 4 − 2 + 6 = 8.
10213243546516left run1+right run7=total8every hand63 left0 right42 left1 right81 left2 right0 left3 right8 beats 6 → new best
best8
The hand with 1 left and 2 right scores 8, more than 6, so it becomes the best hand.
10back21324354take6516left run0+right run12=total12every hand63 left0 right42 left1 right81 left2 right0 left3 rightgive back 1, take 5 → total 12
i3
give back1card 0
take5card 4
total12
Trade the innermost left card, card 0 worth 1, for the next card from the right end, card 4 worth 5. The left part still starts at card 0, so the hand stays legal: 0 left and 3 right. 8 − 1 + 5 = 12.
10213243546516left run0+right run12=total12every hand63 left0 right42 left1 right81 left2 right120 left3 right12 beats 8 → new best
best12
The hand with 0 left and 3 right scores 12, more than 8, so it becomes the best hand.
10213243546516left run0+right run12=total12every hand63 left0 right42 left1 right81 left2 right120 left3 rightall 4 hands compared → answer 12
return12
Every legal hand has been scored. The best is 12, from 0 left and 3 right: the last 3 cards. The 1 middle card was never read.
05

Common pitfalls

Taking the larger end card each time

✗ Wrong
for _ in range(k):
    if cardPoints[lo] >= cardPoints[hi]:
        total += cardPoints[lo]; lo += 1
    else:
        total += cardPoints[hi]; hi -= 1
✓ Right
for i in range(1, k + 1):
    total += cardPoints[n - i] - cardPoints[k - i]
    best = max(best, total)

A cheap end card can hide a valuable one behind it. For [5, 1, 100, 1] with k = 2, the greedy pick takes 5, then a 1, and scores 6. Taking the two right cards, 1 and then 100, scores 101.

Giving back the outer left card

✗ Wrong
total += cardPoints[n - i] - cardPoints[i - 1]
✓ Right
total += cardPoints[n - i] - cardPoints[k - i]

Dropping card 0 first leaves a left part that no longer starts at the left end, which no sequence of picks can produce. For [1, 100, 1, 5] with k = 2, the wrong line builds the hand 100 + 5 and returns 105; the real best is 101.

06

Edge cases

k equals the number of cards

Every card is taken, so every hand is the whole row and every trade swaps a card for itself. The total never changes and the answer is the sum of all cards.

07

Complexity

Time
O(k)
Space
O(1)
The first hand reads k cards and each of the k trades reads two more. Cards in the middle that no hand can reach are never read. Only the running total and the best are stored.
08

Maximum Points You Can Obtain from Cards FAQ

Can I find the cards to leave behind instead?

Yes. The untaken cards are one block of n − k neighbours, so the answer is the sum of all cards minus the smallest sum of any n − k neighbouring cards. It is also correct, but it reads all n cards, while trading reads only the 2k cards at the ends. It is the faster one only when k is more than half of n.

Is dynamic programming needed here?

No. A table over how many cards were taken from each side works, but the state only depends on the number taken from the left, so it shrinks to the k + 1 hands compared here. The table would cost O(k²) for the same answer.

What if I may take at most k cards instead of exactly k?

With every card worth at least 1, taking more cards never lowers the total, so the answer is the same. If cards could be negative, a smaller hand might score more, so hands of every size from 0 to k would need comparing.