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.
- 1 <= cardPoints.length <= 10⁵
- 1 <= cardPoints[i] <= 10⁴
- 1 <= k <= cardPoints.length
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.
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.
Approach
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.
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.
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.
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.
Code in Python, C++ and Java
k is the number of cards.k is the number of cards.k is the number of cards.Common pitfalls
Taking the larger end card each time
for _ in range(k):
if cardPoints[lo] >= cardPoints[hi]:
total += cardPoints[lo]; lo += 1
else:
total += cardPoints[hi]; hi -= 1for 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
total += cardPoints[n - i] - cardPoints[i - 1]
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.
Edge cases
k equals the number of cardsEvery 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.
Complexity
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.