Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Constraints:
1 <= cardPoints.length <= 1051 <= cardPoints[i] <= 1041 <= k <= cardPoints.lengthProblem summary: There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Sliding Window
[1,2,3,4,5,6,1] 3
[2,2,2] 2
[9,7,7,9,7,7,9] 7
maximum-score-from-performing-multiplication-operations)removing-minimum-and-maximum-from-array)minimum-recolors-to-get-k-consecutive-black-blocks)maximum-spending-after-buying-items)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1423: Maximum Points You Can Obtain from Cards
class Solution {
public int maxScore(int[] cardPoints, int k) {
int s = 0, n = cardPoints.length;
for (int i = n - k; i < n; ++i) {
s += cardPoints[i];
}
int ans = s;
for (int i = 0; i < k; ++i) {
s += cardPoints[i] - cardPoints[n - k + i];
ans = Math.max(ans, s);
}
return ans;
}
}
// Accepted solution for LeetCode #1423: Maximum Points You Can Obtain from Cards
func maxScore(cardPoints []int, k int) int {
n := len(cardPoints)
s := 0
for _, x := range cardPoints[n-k:] {
s += x
}
ans := s
for i := 0; i < k; i++ {
s += cardPoints[i] - cardPoints[n-k+i]
ans = max(ans, s)
}
return ans
}
# Accepted solution for LeetCode #1423: Maximum Points You Can Obtain from Cards
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
ans = s = sum(cardPoints[-k:])
for i, x in enumerate(cardPoints[:k]):
s += x - cardPoints[-k + i]
ans = max(ans, s)
return ans
// Accepted solution for LeetCode #1423: Maximum Points You Can Obtain from Cards
impl Solution {
pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {
let n = card_points.len();
let k = k as usize;
let mut s: i32 = card_points[n - k..].iter().sum();
let mut ans: i32 = s;
for i in 0..k {
s += card_points[i] - card_points[n - k + i];
ans = ans.max(s);
}
ans
}
}
// Accepted solution for LeetCode #1423: Maximum Points You Can Obtain from Cards
function maxScore(cardPoints: number[], k: number): number {
const n = cardPoints.length;
let s = cardPoints.slice(-k).reduce((a, b) => a + b);
let ans = s;
for (let i = 0; i < k; ++i) {
s += cardPoints[i] - cardPoints[n - k + i];
ans = Math.max(ans, s);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.