Maximum Points You Can Obtain from Cards

Medium

Topics
ArraySliding WindowPrefix Sum

nums is a row of cards with a value on each. In one step you take a card from either the beginning or the end of the row. You must take exactly k cards.

Return the maximum total value you can collect.

LeetCode names the first argument cardPoints; the harness here calls it nums.

Example 1

10213243546516left behind
takenleft behind
Taking k = 3 of these 7 cards scores 12 at best. Cards only come off the two ends, so whatever you take leaves a single contiguous block in the middle — and every possible set of takings corresponds to exactly one such block. The braced block of 4 cards sums to 10, the least any block of that width can, which is what makes the shaded cards the best haul.
The taken cards are a prefix and a suffix, never a stretch from the middle.
Input:  nums = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: Take 5 and 6 and 1 from the end for 12, which beats taking 1 and 2 and 3 from the front.

Example 2

202122left behind
takenleft behind
Input:  nums = [2,2,2], k = 2
Output: 4
Explanation: Every choice gives the same total.

Example 3

90717293747596
taken
Here k is the whole row, so no block is left and the score is simply the total 55.
Input:  nums = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: Taking every card leaves no choice at all, so the answer is the whole sum.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^4
  • 1 <= k <= nums.length
Editorial — intuition, approach, complexity and pitfalls

Intuition

Taking greedily from whichever end currently shows the larger card is wrong, and it is worth seeing why: a large card may sit behind a small one, and giving up a point now can buy two later. Example 1 is exactly this shape — the front offers 1 against the back’s 1, and the right play is still to work from the back.

The reframing that solves it: taking k cards from the two ends means leaving behind a contiguous block of n - k cards in the middle. Every possible set of taken cards corresponds to exactly one such block, and vice versa.

So maximising what you take is the same as minimising the sum of a contiguous block of fixed length n - k. That is a fixed-width window slid across the array once, which is a much easier question than the original.

Subtract that minimum from the total and you have the answer. The case k == n falls out correctly: the block has length zero, its sum is zero, and the answer is the whole total.

Approach

  1. Compute the total of all cards.
  2. Let w = n - k be the width of the block left behind. If w == 0, return the total.
  3. Sum the first w cards as the initial window.
  4. Slide the window one position at a time, adding the entering card and removing the leaving one, tracking the smallest sum seen.
  5. Return total - smallest window sum.

Walkthrough

nums = [1,2,3,4,5,6,1], k = 3

n = 7, so the block left behind has width 7 - 3 = 4
total = 1+2+3+4+5+6+1 = 22

windows of width 4
   [1,2,3,4]  sum 10
   [2,3,4,5]  sum 14
   [3,4,5,6]  sum 18
   [4,5,6,1]  sum 16

smallest window = 10, which is the front four cards
so the cards TAKEN are the last three: 5, 6, 1
answer 22 - 10 = 12

why the greedy end-pick fails
   front shows 1, back shows 1 — a tie, and whichever
   you pick the next comparison is 2 against 6. A rule
   that only looks one card deep cannot see that the
   back holds 5 and 6 behind that 1.

k = n case
   nums = [9,7,7,9,7,7,9], k = 7
   width 0, no window, answer = total = 55

Complexity

  • Time — O(n): one pass for the total and one for the window.
  • Space — O(1): a running sum and a minimum.

Common Pitfalls

  • Taking greedily from the larger end. It fails whenever a big card sits behind a small one.
  • Getting the block width backwards and sliding a window of k. The block left behind has width n - k.
  • Not handling k == n, where the window is empty and a loop over it does nothing or divides by zero.
  • Recomputing the window sum from scratch at each position, which is quadratic.
  • Trying every split of k into front and back counts. That is also correct and linear, but it is easy to get the index arithmetic wrong at the boundaries.

Full Solution — all languages

class Solution:
    def maxScore(self, nums, k):
        n = len(nums)
        total = sum(nums)

        # Taking k cards from the ends == leaving a contiguous block of n - k.
        width = n - k
        if width == 0:
            return total

        window = sum(nums[:width])
        smallest = window

        for i in range(width, n):
            window += nums[i] - nums[i - width]
            smallest = min(smallest, window)

        return total - smallest
SensAI
Beta
Tap the mic to stop voice mode