Show problems as
Classic problem #837MediumMathDynamic ProgrammingSliding WindowProbability and Statistics

New 21 Game

Real-world version: Token Counter Stop Rule
Tracked in this browser only
Read New 21 Game on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A fairground machine keeps a running tally of tokens, starting at 0.

The machine works like this. While the tally is strictly below k, it dispenses one more handful and adds it to the tally. A handful holds a whole number of tokens drawn from 1 to maxPts, and every one of those maxPts amounts is equally likely, independently of anything dispensed before. As soon as the tally is k or more the machine stops for good. If the tally already meets k before anything is dispensed, the machine never dispenses at all.

Return the probability that the tally the machine stops on is at most n, rounded to 6 decimal places.

Examples

Example 1

Input
n = 5, k = 1, maxPts = 10
Output
0.5

One handful is dispensed and then the tally is at least 1, so the machine stops. Five of the ten equally likely amounts, namely 1 through 5, leave a tally of at most 5.

Example 2

Input
n = 4, k = 5, maxPts = 1
Output
0.0

Every handful holds exactly one token, so the machine stops the moment the tally reaches 5, and 5 is above 4.

Example 3

Input
n = 12, k = 3, maxPts = 6
Output
1.0

The machine can stop on any tally from 3 to 8, and 12 is above all of them, so the answer is 1.

Constraints

  • 0 <= k <= n <= 10^4
  • 1 <= maxPts <= 10^4
  • The answer is the exact probability rounded to 6 decimal places.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def new21_game(n: int, k: int, maxPts: int) -> float:
Java
public double new21Game(int n, int k, int maxPts)