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

# New 21 Game Python Solution with Tests

> Tested Python solution for LeetCode 837 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 837, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), [Sliding Window](/catalog/topics/sliding-window), Probability and Statistics. [View on LeetCode](https://leetcode.com/problems/new-21-game/description/).

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

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

## Problem

Alice plays the following game, loosely based on the card game `"21"`.

Alice starts with `0` points and draws numbers while she has less than `k` points. During each draw, she gains an integer number of points randomly from the range `[1, maxPts]`, where `maxPts` is an integer. Each draw is independent and the outcomes have equal probabilities.

Alice stops drawing numbers when she gets `k` or more points.

Return the probability that Alice has `n` or fewer points.

Answers within `10-5` of the actual answer are considered accepted.

### Examples

```
Input: n = 10, k = 1, maxPts = 10
Output: 1.00000
Explanation: Alice gets a single card, then stops.
```

```
Input: n = 6, k = 1, maxPts = 10
Output: 0.60000
Explanation: Alice gets a single card, then stops.
In 6 out of 10 possibilities, she is at or below 6 points.
```

```
Input: n = 21, k = 17, maxPts = 10
Output: 0.73278
```

### Constraints

* 0 \<= k \<= n \<= 10^4
* 1 \<= maxPts \<= 10^4

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + k)
    # Space: O(n)
    def new21_game(self, n: int, k: int, max_pts: int) -> float:
        if k == 0:
            return 1.0
        # dp[i]: probability of ending with exactly i points; dp[i] is the
        # mean of dp[i - max_pts .. min(i - 1, k - 1)], kept as a running
        # window sum since only pre-stop states seed further draws
        dp = [0.0] * (n + 1)
        dp[0] = 1.0
        window = 1.0
        for i in range(1, n + 1):
            dp[i] = window / max_pts
            if i < k:
                window += dp[i]
            if i - max_pts >= 0 and i - max_pts < k:
                window -= dp[i - max_pts]
        return sum(dp[k : n + 1])
```

## Complexity

| Time | Space |
| - | - |
| O(n + k) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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