Maximum Points You Can Obtain from Cards
Pattern: Sliding window: fixed size
Taking k cards from the two ends is the same as choosing which k + 1 splits to try. Start with all k from the left, then swap one left card for one right card at a time.
The problem
Cards stand in a row, each with some points. You take exactly k cards, one at a time, and each card must come from the
left end or the right end of what remains. Return the largest total you can collect. For example, [1, 2, 3, 4, 5, 6, 1]
with k = 3 gives 12 (take 1, 6 and 5 from the right), and [9, 7, 7, 9, 7, 7, 9] with k = 7 gives 55 (take every
card).
The function name maxScore matches LeetCode. Use the link above for the exact statement and limits.
Think first
- Does the order in which you take the cards matter, or only how many come from each end?
- If
icards come from the left, how many come from the right? How many different splits are there? - Going from "i from the left" to "i - 1 from the left", which single card leaves and which single card joins?
Hints
Brute force, and why it fails
Try every order of taking cards from either end: 2^k sequences, far too many. Trying all k + 1 splits and adding each
from scratch is O(k²). Sliding the split one card at a time is O(k).
The key insight
The taken cards are a prefix plus a suffix of total length k. Neighbouring splits differ by one card on each side, so a
running sum moves from one split to the next in O(1).
Dry run
| sum | 6 |
|---|---|
| best | 6 |
cards = [1, 2, 3, 4, 5, 6, 1] and k = 3. Start by taking the first 3 cards from the left. The sum is 1 + 2 + 3 = 6.
The plan
sum = cards[0] + ... + cards[k - 1] # all k from the left
best = sum
for i from 1 to k:
sum = sum - cards[k - i] # give back a left card
sum = sum + cards[n - i] # take a right card
best = max(best, sum)
return best
Complexity
- Time
O(k)- Space
O(1)
One pass over the first k cards, then k single-card swaps. Only two numbers are stored.
Edge cases
k = n: every card is taken, and the answer is the total.k = 1: the larger of the two end cards.- A huge card in the middle, as in
[1, 1000, 1]withk = 1: it cannot be reached, so the answer is 1.
I've built my logic, show the code
Java
class Solution {
public int maxScore(int[] cardPoints, int k) {
int n = cardPoints.length;
int sum = 0;
for (int i = 0; i < k; i++) {
sum += cardPoints[i]; // start by taking all k cards from the left
}
int best = sum;
for (int i = 1; i <= k; i++) {
sum -= cardPoints[k - i]; // give back the innermost left card
sum += cardPoints[n - i]; // and take one more from the right
best = Math.max(best, sum);
}
return best;
}
}C++
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int n = static_cast<int>(cardPoints.size());
int sum = 0;
for (int i = 0; i < k; i++) {
sum += cardPoints[i]; // start by taking all k cards from the left
}
int best = sum;
for (int i = 1; i <= k; i++) {
sum -= cardPoints[k - i]; // give back the innermost left card
sum += cardPoints[n - i]; // and take one more from the right
best = max(best, sum);
}
return best;
}
};Python
class Solution:
def maxScore(self, cardPoints: list[int], k: int) -> int:
n = len(cardPoints)
total = sum(cardPoints[:k]) # start by taking all k cards from the left
best = total
for i in range(1, k + 1):
total -= cardPoints[k - i] # give back the innermost left card
total += cardPoints[n - i] # and take one more from the right
best = max(best, total)
return bestCommon mistakes
- Greedily taking the bigger end card each turn. In
[2, 100, 1, 1, 3]withk = 2, greed takes 3 and then 2 for 5, but taking 2 and then 100 from the left gives 102. - Off-by-one in the indexes: the card given back is
cards[k - i], and the card taken iscards[n - i]. - Forgetting the split with all
kfrom the left, which is the starting value ofbest.