2Brute2Best
🔥0

Maximum Points You Can Obtain from Cards

Medium·LeetCode #1423 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

Some cards are laid out in a row, and card i is worth cardPoints[i] points.

You make exactly k moves. In each move you take one card from either end of the row — the leftmost or the rightmost card that is still there. Your score is the total of the cards you took.

Return the highest score you can get.

Input

An integer array cardPoints and an integer k.

Output

The maximum total points from taking exactly k cards, each from the left or right end.

Example 1
Input: cardPoints = [1, 2, 3, 4, 5, 6, 1], k = 3
Output: 12

Take the three rightmost cards: 1 + 6 + 5 = 12. No other choice does better.

Example 2
Input: cardPoints = [2, 2, 2], k = 2
Output: 4

Any two cards are worth 2 + 2 = 4.

Example 3
Input: cardPoints = [9, 7, 7, 9, 7, 7, 9], k = 7
Output: 55

k equals the number of cards, so you take all of them: the total is 55.

Constraints
  • • 1 ≤ cardPoints.length ≤ 10^5
  • • 1 ≤ cardPoints[i] ≤ 10^4
  • • 1 ≤ k ≤ cardPoints.length
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“take from the beginning or the end”
→ Order doesn't matter; you end with a prefix + a suffix. What remains is a contiguous middle block.
“exactly k cards”
→ The leftover block always has exactly n − k cards → fixed window size.
“maximum score”
→ Max taken = total − min left → minimise the window sum.
03

Approach Complexity Ladder

Brute Force — try every left/right splitO(K²)O(1)

For each i in 0..k, add the first i and last k − i cards from scratch

Optimal — minimise the leftover middle windowO(N)O(1)

Total minus the smallest sum of a size n − k window

Brute Force Intuition

Any sequence of moves ends with some i cards taken from the left and k − i from the right. Try every i from 0 to k, add up those cards from scratch, and keep the best total.

Recognition rule for this sub-pattern:

Window size is fixed at K. Each slide adds one element on the right and removes one on the left — update the aggregate in O(1).
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.