↓Skip to main content
  1. LeetCode/

LeetCode 1423: Maximum Points You Can Obtain from Cards

·1 min· ·
LeetCode Medium Sliding-Window Prefix-Sum Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-12-24

  • Submissions: Accepted (first try)

Approach
#

Cards come only from the front or back, so after taking k, what remains is always a contiguous middle of n - k cards.

So the maximum taken = total − the minimum sum of a contiguous subarray of length n - k, found with a fixed-size sliding window.

My Solution
#

class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        n = len(cardPoints)
        size = n - k                               # cards left behind
        window = sum(cardPoints[:size])
        smallest = window
        for right in range(size, n):
            window += cardPoints[right] - cardPoints[right - size]
            smallest = min(smallest, window)
        return sum(cardPoints) - smallest

With k == n, size = 0, the window stays 0, and the answer is the total.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Greedily taking the larger end each time: earlier picks change what is available later, so it is not always optimal.

Interview Explanation
#

Taking k cards from the ends leaves a contiguous middle of n - k cards. So I find the minimum sum of a length-n - k subarray with a fixed sliding window, and the answer is the total minus that. Time O(n).

Related

LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 1658: Minimum Operations to Reduce X to Zero
·3 mins
LeetCode Daily Medium Array Sliding-Window Prefix-Sum Reverse-Thinking
LeetCode Problem Solving - Reverse Thinking with Sliding Window
LeetCode 1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
·3 mins
LeetCode Daily Medium Array Sliding-Window Prefix-Sum Average-Calculation
LeetCode Problem Solving - Fixed Size Sliding Window with Sum Calculation
LeetCode 2461: Maximum Sum of Distinct Subarrays With Length K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2461 note: in a fixed window of length k, all values are distinct exactly when the number of distinct values equals k
LeetCode 1493: Longest Subarray of 1's After Deleting One Element
·2 mins
LeetCode Medium Sliding-Window Dynamic-Programming Array
LeetCode 1493 note: find the longest window with at most one 0; since one element must be deleted, the answer is the window length minus 1