Maximum Points You Can Obtain from Cards (LeetCode 1423): Solution and Explanation
Take exactly k cards, each from the left or right end of a row, and return the largest total you can collect.
Intuition
The order of taking does not matter. Any sequence of k picks from the two ends leaves you holding some prefix of the row plus some suffix, with k cards in total.
So there are only k + 1 possible hands: k left cards and 0 right, then k - 1 and 1, down to 0 and k. Trying all of them with fresh sums would cost O(k^2).
Neighbouring hands differ by one card. To move from i - 1 right cards to i, give back the innermost left card k - i and take the outer right card n - i. The total changes by two numbers.
Another way to see it: the cards left on the table are a contiguous window of length n - k, and the best hand is the total minus the cheapest such window. Both views slide a window; this one only touches 2k cards.
Approach: Swap one left card for one right card
- Let
totalbe the sum of the first k cards and setbest = total. - For
ifrom 1 to k, subtractcardPoints[k - i]fromtotal. - Add
cardPoints[n - i]tototal. - Set
best = max(best, total). - Return
best.
Step-by-step example
cardPoints = [1,2,3,4,5,6,1], k = 3
- Start with the left three cards 1, 2, 3: total = 6, best = 6.
- i = 1: give back card 2 (3 points) and take card 6 (1 point). Hand 1, 2 | 1, total = 4.
- i = 2: give back card 1 (2) and take card 5 (6). Hand 1 | 6, 1, total = 8, best = 8.
- i = 3: give back card 0 (1) and take card 4 (5). Hand 5, 6, 1, total = 12, best = 12. The answer is 12.
Code: Python, Java, C++
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[k - i]
8 total += cardPoints[n - i]
9 best = max(best, total)
10 return best1class Solution {
2 public int maxScore(int[] cardPoints, int k) {
3 int n = cardPoints.length;
4 int total = 0;
5 for (int i = 0; i < k; i++) total += cardPoints[i];
6 int best = total;
7 for (int i = 1; i <= k; i++) {
8 total -= cardPoints[k - i];
9 total += cardPoints[n - i];
10 best = Math.max(best, total);
11 }
12 return best;
13 }
14}1class Solution {
2public:
3 int maxScore(vector<int>& cardPoints, int k) {
4 int n = cardPoints.size();
5 int total = 0;
6 for (int i = 0; i < k; i++) total += cardPoints[i];
7 int best = total;
8 for (int i = 1; i <= k; i++) {
9 total -= cardPoints[k - i];
10 total += cardPoints[n - i];
11 best = max(best, total);
12 }
13 return best;
14 }
15};Time and space complexity
O(k)
The first sum touches k cards and the loop makes k swaps of O(1) each.
O(1)
Only the running total and the best total are stored.
Other approaches
Minimum window left on the table (time O(n), space O(1))
Compute the total of all cards, then slide a window of length n - k across the row and find its smallest sum. The answer is total minus that minimum. This is O(n) rather than O(k).
Prefix and suffix sums (time O(k), space O(k))
Build prefix sums of the first k cards and suffix sums of the last k, then take the max of prefix[i] + suffix[k - i] over all i.
Recursion choosing an end (time O(2^k), space O(k))
Try taking the left or right card at every step. It explores 2^k orders that lead to the same k + 1 hands, so it times out quickly.
Common mistakes
- Being greedy and taking the bigger end card each step. With [1,79,80,1,1,1,200,1] and k = 3 greedy takes 1, 200, 1 = 202, but on other inputs a small end card hides big cards behind it and greedy loses.
- Giving back card
i - 1instead ofk - i. The card you return must be the innermost one of the left part, the last one you took from that side. - Forgetting the hand with all k cards from the left, which is the starting value of
best. - In the window version, mishandling k = n, where the window on the table is empty and the answer is the full sum.
Frequently asked questions
Why can't the order of taking cards matter?
Taking a card from the left only ever removes the next card of the prefix, and the same for the right. However you interleave the picks, you end with a prefix and a suffix whose lengths add to k.
Which version is better, O(k) or O(n)?
Swapping from the ends touches only 2k cards, so it is faster when k is much smaller than n. The window-on-the-table version is easier to explain to some people and is O(n). Both use O(1) space.
Can the total overflow?
At most 10^5 cards of 10^4 points each sum to 10^9, which fits in a signed 32-bit integer.