Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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.
Example 1:
Input: n = 10, k = 1, maxPts = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops.
Example 2:
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.
Example 3:
Input: n = 21, k = 17, maxPts = 10 Output: 0.73278
Constraints:
0 <= k <= n <= 1041 <= maxPts <= 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming · Sliding Window
10 1 10
6 1 10
21 17 10
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #837: New 21 Game
class Solution {
private double[] f;
private int n, k, maxPts;
public double new21Game(int n, int k, int maxPts) {
f = new double[k];
this.n = n;
this.k = k;
this.maxPts = maxPts;
return dfs(0);
}
private double dfs(int i) {
if (i >= k) {
return i <= n ? 1 : 0;
}
if (i == k - 1) {
return Math.min(n - k + 1, maxPts) * 1.0 / maxPts;
}
if (f[i] != 0) {
return f[i];
}
return f[i] = dfs(i + 1) + (dfs(i + 1) - dfs(i + maxPts + 1)) / maxPts;
}
}
// Accepted solution for LeetCode #837: New 21 Game
func new21Game(n int, k int, maxPts int) float64 {
f := make([]float64, k)
var dfs func(int) float64
dfs = func(i int) float64 {
if i >= k {
if i <= n {
return 1
}
return 0
}
if i == k-1 {
return float64(min(n-k+1, maxPts)) / float64(maxPts)
}
if f[i] > 0 {
return f[i]
}
f[i] = dfs(i+1) + (dfs(i+1)-dfs(i+maxPts+1))/float64(maxPts)
return f[i]
}
return dfs(0)
}
# Accepted solution for LeetCode #837: New 21 Game
class Solution:
def new21Game(self, n: int, k: int, maxPts: int) -> float:
@cache
def dfs(i: int) -> float:
if i >= k:
return int(i <= n)
if i == k - 1:
return min(n - k + 1, maxPts) / maxPts
return dfs(i + 1) + (dfs(i + 1) - dfs(i + maxPts + 1)) / maxPts
return dfs(0)
// Accepted solution for LeetCode #837: New 21 Game
struct Solution;
impl Solution {
fn new21_game(n: i32, k: i32, w: i32) -> f64 {
if k == 0 || n > k + w {
return 1.0;
}
let n = n as usize;
let w = w as usize;
let k = k as usize;
let mut dp: Vec<f64> = vec![0.0; n + 1];
dp[0] = 1.0;
let mut sum = 1.0;
let mut res = 0.0;
for i in 1..=n {
dp[i] = sum / w as f64;
if i < k {
sum += dp[i];
} else {
res += dp[i];
}
if i >= w {
sum -= dp[i - w];
}
}
res
}
}
#[test]
fn test() {
use assert_approx_eq::assert_approx_eq;
let n = 10;
let k = 1;
let w = 10;
let res = 1.0;
assert_approx_eq!(Solution::new21_game(n, k, w), res);
let n = 6;
let k = 1;
let w = 10;
let res = 0.6;
assert_approx_eq!(Solution::new21_game(n, k, w), res);
let n = 21;
let k = 17;
let w = 10;
let res = 0.732777;
assert_approx_eq!(Solution::new21_game(n, k, w), res);
}
// Accepted solution for LeetCode #837: New 21 Game
function new21Game(n: number, k: number, maxPts: number): number {
const f: number[] = Array(k).fill(0);
const dfs = (i: number): number => {
if (i >= k) {
return i <= n ? 1 : 0;
}
if (i === k - 1) {
return Math.min(n - k + 1, maxPts) / maxPts;
}
if (f[i] !== 0) {
return f[i];
}
return (f[i] = dfs(i + 1) + (dfs(i + 1) - dfs(i + maxPts + 1)) / maxPts);
};
return dfs(0);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.