Stop or Roll: Ten-Sided Die With a One-Time Reset
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Decide whether to stop or roll in a dice accumulation game where the first zero resets your total and the second zero wipes you out, using DP over (total, reset-used) states.
You are playing a solo dice game with a fair ten-sided die whose faces are 0, 1, 2, ..., 9, each equally likely. Rolls are independent.
You hold a running total. On each turn you must pick one of two actions:
- stop: the game ends and you are paid exactly your current total in cash.
- roll: roll the die once.
- Faces
1through9add that value to your total, and you then face the same choice again. - Face
0depends on whether a zero has already been rolled earlier in this game:- If this is the first zero, your total is reset to
0(nothing is banked) and the game continues. You may stop or roll again from0. - If this is the second zero, the game ends immediately and you are paid
0.
- If this is the first zero, your total is reset to
- Faces
Rolling costs nothing, and there is no limit on the number of rolls. Your goal is to maximize your expected cash payout.
Given the current total and whether the first zero has already happened, return the optimal action for that state.
Write a function that takes:
total: the current running total (an integer).reset_used:Trueif the first zero has already occurred in this game,Falseotherwise.
Return the string "stop" if stopping now yields an expected payout at least as large as rolling (ties go to "stop"). Otherwise return "roll".
Constraints
0 <= total <= 1000reset_usedis a boolean.- The objective is expected payout, not the probability of reaching a target.
- A reset does not bank the previous total, and you are allowed to stop immediately after a reset (with a total of
0). - Both optimal play from every future state and the tie-breaking rule (stop when equal) must be used.
Examples
Example 1
Input: best_move(0, False)
Output: 'roll'
Example 2
Input: best_move(10, False)
Output: 'roll'
Example 3
Input: best_move(0, True)
Output: 'roll'
Example 4
Input: best_move(10, True)
Output: 'roll'
Hints
Hint 1
The right state is the pair (total, whether a zero has already happened). Solve the 'zero already happened' case first, since it never depends on the other case.
Hint 2
In the 'zero already happened' case, rolling a 0 pays 0. For large totals stopping beats rolling, so you can compute values downward from a large cap: value(t) = max(t, average over the ten faces of the next values).
Hint 3
In the 'no zero yet' case, rolling a 0 does not end the game: it leads to the value of total 0 in the 'zero already happened' world. Plug that constant in, run the same downward DP, and compare stopping with rolling at the queried total.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The decision depends only on two things: the current total and whether the first zero has already been used. Both are small, so we can compute the value of every state by dynamic programming.
-
Zero already used. Rolling a 0 ends the game with payout 0. Let
A(t)be the best expected payout at totalt. ThenA(t) = max(t, (0 + A(t+1) + ... + A(t+9)) / 10). For larget, stopping is better than rolling, so we fix a large cap and fill the table from the cap downward to0. -
Zero not yet used. Rolling a 0 resets the total to 0 but the game continues, and it is now in the "zero used" world. That outcome is worth
A(0). LetB(t)be the value here:B(t) = max(t, (A(0) + B(t+1) + ... + B(t+9)) / 10). -
For the queried state, compute the expected value of rolling from the correct table and compare it with
total. Iftotal >= roll value, answer"stop"; otherwise"roll".
Why it is correct
The game is a Markov decision process whose state is (total, zero-used flag). Totals only increase except at the one reset, and the reset moves to the other flag, so the "zero used" table has no cyclic dependency and can be solved first; the "no zero yet" table then depends only on already-known values plus larger totals. Beyond the cap, rolling is strictly worse than stopping, so truncating there does not change the decision for any total in range. Taking the max of stopping and rolling at each state is the Bellman optimality condition, so comparing the two options at the queried state gives the optimal action. Ties are broken toward stopping, as the statement requires.
Complexity
Each table has about L entries (here L = 1000) and each entry sums 9 neighbors, so time is O(9 * L) per call and space is O(L).
Reference solution
from typing import List
def best_move(total: int, reset_used: bool) -> str:
limit = 1000
after_reset: List[float] = [0.0] * (limit + 10)
before_reset: List[float] = [0.0] * (limit + 10)
for t in range(limit, -1, -1):
roll_val = (0.0 + sum(after_reset[t + f] for f in range(1, 10))) / 10
after_reset[t] = max(float(t), roll_val)
reset_value = after_reset[0]
for t in range(limit, -1, -1):
roll_val = (reset_value + sum(before_reset[t + f] for f in range(1, 10))) / 10
before_reset[t] = max(float(t), roll_val)
if reset_used:
table = after_reset
zero_outcome = 0.0
else:
table = before_reset
zero_outcome = reset_value
roll_value = (zero_outcome + sum(table[total + f] for f in range(1, 10))) / 10
return "stop" if float(total) >= roll_value else "roll"