Score Bust Probability: Stop When You Hit the Target
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Tests whether a candidate can build an O(n) sliding-window DP to compute the probability that a random accumulation process stops within a given score ceiling.
Problem
You are simulating a simple scoring game. A player starts at 0 points and keeps rolling a fair die that produces an integer uniformly at random between 1 and sides (inclusive). After each roll the result is added to the player's running total. The player stops the moment their total reaches or exceeds goal.
Return the probability that the player's final total is at most ceiling.
Rules
- If
goalis0, the player never rolls. Their final total is0, so the probability is1.0. - An answer is accepted if it differs from the exact probability by no more than
1e-5in absolute terms.
Constraints
0 <= goal <= ceiling <= 10^41 <= sides <= 10^4
Examples
Example 1
Input: score_within_ceiling(6, 1, 10)
Output: 0.6
Example 2
Input: score_within_ceiling(2, 2, 2)
Output: 0.75
Example 3
Input: score_within_ceiling(4, 3, 3)
Output: 0.8518518518518519
Hints
Hint 1
Define dp[x] as the probability that the running total lands on exactly x at some point during the game. Only totals below goal can trigger another roll — totals at or above goal are terminal.
Hint 2
The recurrence dp[x] = (1/sides) * sum(dp[y] for y in [x-sides, x-1] if y < goal) looks like an O(n * sides) computation, but the inner sum changes by at most two terms as x increases by one — that's a sliding window.
Hint 3
Be careful about which states feed the window. A terminal state (total >= goal) must never be added back into the window; including it would inflate every subsequent probability. Handle goal == 0 up front to avoid an off-by-one on the first iteration.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Defining the state
Let dp[x] be the probability that the running total lands on exactly x at any point during the game. Because the player only rolls from totals strictly below goal, the transitions flow in one direction: a non-terminal total y < goal sends probability dp[y] / sides to each of y+1, y+2, ..., y+sides.
Starting from dp[0] = 1, this gives the recurrence:
dp[x] = (1 / sides) * sum(dp[y] for y in [x - sides, x - 1] if y < goal)
The final answer is the sum of dp[x] for all terminal totals x in [goal, ceiling]. These events are disjoint — the game ends at exactly one value — so summing them is valid.
From O(n · sides) to O(n) with a sliding window
The inner sum spans at most sides consecutive values of y, and as x increases by one the window shifts by one position. That means each step adds at most one new term and removes at most one old term, giving O(1) work per index.
The one subtlety: a terminal total (x >= goal) must not enter the window. Once the player stops, that state can never trigger another roll, so including it would incorrectly inflate every subsequent dp value. This is the most common bug in naive implementations.
Edge cases
goal == 0: no rolls happen; the final total is0; return1.0.ceiling >= goal - 1 + sides: even the largest possible final total fits under the ceiling; return1.0.sides == 1: every roll adds 1, so the game always ends at exactlygoal; the window shortcut handles this correctly.- Large
sidesrelative togoal: the window never needs to drop anything early; thex - sides >= 0guard handles this.
Correctness check on Example 3
ceiling = 4, goal = 3, sides = 3:
| x | dp[x] | role | window after |
|---|---|---|---|
| 0 | 1 | initial | 1 |
| 1 | 1/3 | drawing | 4/3 |
| 2 | 4/9 | drawing | 16/9 |
| 3 | 16/27 | terminal | 16/9 − 1 = 7/9 |
| 4 | 7/27 | terminal | — |
Answer = 16/27 + 7/27 = 23/27 ≈ 0.85185 ✓
Complexity
- Time: O(ceiling) — one pass with O(1) work per index.
- Space: O(ceiling) for the
dparray. A circular buffer of sizesideswould reduce this to O(min(ceiling, sides)), but the plain array is well within limits at 10^4.
Reference solution
def score_within_ceiling(ceiling: int, goal: int, sides: int) -> float:
# If goal is 0 the player never rolls, final total is 0, probability is 1.
# If the highest possible stopping total (goal - 1 + sides) is still within
# the ceiling, every outcome qualifies and the probability is also 1.
if goal == 0 or ceiling >= goal - 1 + sides:
return 1.0
# dp[x] = probability the running total ever equals exactly x.
dp = [0.0] * (ceiling + 1)
dp[0] = 1.0
# window = sum of dp[y] for the last `sides` indices y where y < goal.
# These are the only states that produce another roll and can lead to x.
window = 1.0
ans = 0.0
for x in range(1, ceiling + 1):
dp[x] = window / sides
if x < goal:
# This total is not terminal: the player will roll again from here,
# so it must contribute to future totals via the window.
window += dp[x]
else:
# This total is terminal: accumulate it into the answer.
ans += dp[x]
# The oldest entry in the window (x - sides) can no longer reach x + 1,
# so slide it out — but only if it was ever a drawing state.
if 0 <= x - sides < goal:
window -= dp[x - sides]
return ans