> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Points You Can Obtain from Cards

> Tested Python solution for LeetCode 1423 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1423, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1423   # by problem number
lcpy gen -s maximum_points_you_can_obtain_from_cards   # by problem name
```

## Problem

There are several cards \<strong>arranged in a row\</strong>, and each card has an associated number of points. The points are given in the integer array \<code>cardPoints\</code>.

In one step, you can take one card from the beginning or from the end of the row. You have to take exactly \<code>k\</code> cards.

Your score is the sum of the points of the cards you have taken.

Given the integer array \<code>cardPoints\</code> and the integer \<code>k\</code>, return the \<em>maximum score\</em> you can obtain.

### Examples

```
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.

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

**Explanation:** Regardless of which two cards you take, your score will always be 4.

```
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 <= 10^5`
* `1 <= cardPoints[i] <= 10^4`
* `1 <= k <= cardPoints.length`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_points_you_can_obtain_from_cards/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_points_you_can_obtain_from_cards/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(k)
    # Space: O(1)
    def max_score(self, card_points: list[int], k: int) -> int:
        window = sum(card_points[:k])
        best = window
        for i in range(1, k + 1):
            window += card_points[-i] - card_points[k - i]
            best = max(best, window)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(k) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.