#1423

Maximum Points You Can Obtain from Cards

Medium Window on the complement

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Cards are in a row. Take exactly k cards, each from either the left or right end. Maximise the sum of taken cards.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: take k cards from the two ends to maximise points.

Why this works: whatever you take, the cards left form one block of n - k in the middle. Minimise that block's sum.

Example: [1, 2, 3, 4, 5, 6, 1], k = 3, total 22

windows of length 4: 10, 14, 18, 16smallest 10 → 22 - 10 = 12

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Trying greedy "take the bigger end each time" — it fails when a big card hides behind a small one.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Sliding Window
🎬 Episode 10: video coming soon