All problems
0575MediumMathDynamic ProgrammingSliding WindowProbability and Statistics

Token Counter Stop Rule

Tracked in this browser only
Write code

Trains the technique from

LeetCode 837New 21 Game

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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