Maximize Points You Can Get from Cards (Leetcode 1423)

MediumLeetcode
⭐⭐⭐⭐

Maximize Points You Can Get from Cards (Leetcode 1423)

Pattern:

Idea:

Variations :


πŸ’» Code

def maxScore(cardPoints, k):
    n = len(cardPoints)

    if k == n:
        return sum(cardPoints)

    window = n - k
    total = sum(cardPoints)

    curr = sum(cardPoints[:window])
    min_sum = curr

    for i in range(window, n):
        curr += cardPoints[i]
        curr -= cardPoints[i - window]
        min_sum = min(min_sum, curr)

    return total - min_sum

Time complexity - O(n)

Aux. Space complexity - O(1)


Maximize Points You Can Get from Cards (Leetcode 1423)

Tags: #SlidingWindow #TwoPointers #Arrays #Optimization #FixedWindow #Interview-Pattern #LeetCode #FAANG

Problem Statement

You are given an integer array cardPoints and an integer k.

In one operation, you can pick one card from either the beginning or the end of the array. You must pick exactly k cards.

Return the maximum score obtainable.

Example

  • cardPoints = [1,2,3,4,5,6,1]

  • k = 3

  • Answer: 12 (pick 6 + 5 + 1)


Key Idea

Instead of maximizing the picked cards, minimize the cards left behind.

If exactly k cards are picked, then the remaining cards form one contiguous subarray of length:

Therefore:

This converts an end-picking problem into a fixed-size sliding window problem.


Intuition (The WHY)

Example:

Cards: [1, 2, 3, 4, 5, 6, 1]
Pick 3 cards

Whatever you pick from the ends, the middle remains contiguous.

Example:

Pick: [1,2]          [6]
Keep:      [3,4,5]

Instead of exploring every left/right combination:

  • Find the minimum sum contiguous window of size n-k.

  • Remove it from the total.

Maximum picked = Total βˆ’ Minimum kept


Optimal Approach β€” Fixed-Size Sliding Window

Algorithm

  1. Compute window_size = n - k.

  2. Compute the total array sum.

  3. Find the minimum window sum of size window_size.

  4. Return total - min_window.

Python Solution

def maxScore(cardPoints, k):
    n = len(cardPoints)

    if k == n:
        return sum(cardPoints)

    window = n - k
    total = sum(cardPoints)

    curr = sum(cardPoints[:window])
    min_sum = curr

    for i in range(window, n):
        curr += cardPoints[i]
        curr -= cardPoints[i - window]
        min_sum = min(min_sum, curr)

    return total - min_sum

Dry Run

cardPoints = [1,2,3,4,5,6,1]

k = 3

Total:

Window size:

Find the minimum window of length 4.

WindowSum
[1,2,3,4]10
[2,3,4,5]14
[3,4,5,6]18
[4,5,6,1]16

Minimum = 10

Answer:


Alternative Sliding Window (Left ↔ Right Exchange)

Another interview-friendly approach starts with taking all k cards from the left, then gradually replaces one left card with one right card.

def maxScore(cardPoints, k):
    n = len(cardPoints)

    curr = sum(cardPoints[:k])
    ans = curr

    for i in range(1, k + 1):
        curr -= cardPoints[k - i]
        curr += cardPoints[n - i]
        ans = max(ans, curr)

    return ans

Why It Works

Initially:

Take: [1,2,3]

Then exchange cards:

Left TakenRight TakenScore
306
214
128
0312

It explores all k+1 possible left/right splits.


Which Approach Is Better?

ApproachIdeaTimeSpace
Min WindowKeep smallest middleO(n)O(1)
Left-Right ExchangeEnumerate all splitsO(k)O(1)
  • Min Window is more reusable and connects to many optimization problems.

  • Exchange is elegant when the interviewer emphasizes end-picking.


Complexity

Minimum Window Approach

MetricValue
TimeO(n)
Auxiliary SpaceO(1)

Left-Right Exchange

MetricValue
TimeO(k)
Auxiliary SpaceO(1)

Relationship to Previous Problems

ProblemTransformation
Minimum Operations to Reduce XKeep longest middle subarray
Max Points from CardsKeep minimum middle window
Sliding Window MaximumFixed-size window optimization

Both LC 1658 and LC 1423 use the same interview trick:

When operations happen only at the ends, think about the contiguous middle that remains.

The only difference is what you optimize:

  • 1658: Maximize kept length

  • 1423: Minimize kept sum


Common Mistakes

1. Brute Forcing Left/Right Choices

There are 2^k possible pick sequences.

The insight is that only k+1 unique left/right splits exist.

2. Forgetting k == n

If every card must be picked:

return sum(cardPoints)

Otherwise the window size becomes zero.

3. Maximizing the Window Instead of Minimizing It

We are maximizing the picked score, so the remaining window must have the minimum possible sum.


Key Takeaways / Pattern Recognition

  • Pick from ends β‡’ The remaining elements are one contiguous subarray.

  • Exactly k picks β‡’ Remaining window size is n-k.

  • Convert:

    • Max picked β†’ Total βˆ’ Min remaining
  • This is a classic optimization-by-complement pattern that pairs naturally with Minimum Operations to Reduce X to Zero (LC 1658).

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon