Maximum Points You Can Obtain from Cards
Medium
Topics
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
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.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
Input: nums = [2,2,2], k = 2 Output: 4 Explanation: Every choice gives the same total.
Example 3
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
- Compute the total of all cards.
- Let
w = n - kbe the width of the block left behind. Ifw == 0, return the total. - Sum the first
wcards as the initial window. - Slide the window one position at a time, adding the entering card and removing the leaving one, tracking the smallest sum seen.
- 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 = 55Complexity
- 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 widthn - 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
kinto 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 - smallestimport java.util.*;
class Solution {
public int maxScore(int[] nums, int k) {
int n = nums.length;
int total = 0;
for (int v : nums) total += v;
int width = n - k;
if (width == 0) return total;
int window = 0;
for (int i = 0; i < width; i++) window += nums[i];
int smallest = window;
for (int i = width; i < n; i++) {
window += nums[i] - nums[i - width];
smallest = Math.min(smallest, window);
}
return total - smallest;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int maxScore(vector<int>& nums, int k) {
int n = (int)nums.size();
int total = 0;
for (int v : nums) total += v;
int width = n - k;
if (width == 0) return total;
int window = 0;
for (int i = 0; i < width; i++) window += nums[i];
int smallest = window;
for (int i = width; i < n; i++) {
window += nums[i] - nums[i - width];
smallest = min(smallest, window);
}
return total - smallest;
}
};/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/ var maxScore = function(nums, k) {
const n = nums.length;
let total = 0;
for (const v of nums) total += v;
const width = n - k;
if (width === 0) return total;
let window = 0;
for (let i = 0; i < width; i++) window += nums[i];
let smallest = window;
for (let i = width; i < n; i++) {
window += nums[i] - nums[i - width];
if (window < smallest) smallest = window;
}
return total - smallest;
};