Alice plays a game loosely based on the card game 21. She 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⁻⁵ of the actual answer are considered accepted.

Input & Output

Example 1 — Basic Case
$ Input: n = 21, k = 17, maxPts = 10
Output: 0.73278
💡 Note: Alice draws until reaching 17+ points. She needs to end with ≤21 points. Most outcomes from 17-26 points are ≤21, giving high probability.
Example 2 — Edge Case
$ Input: n = 6, k = 1, maxPts = 10
Output: 0.60000
💡 Note: Alice stops after first draw (≥1 point). Draws 1-6 are valid (6 outcomes), draws 7-10 exceed n (4 outcomes). Probability = 6/10 = 0.6.
Example 3 — Guaranteed Win
$ Input: n = 21, k = 17, maxPts = 3
Output: 1.00000
💡 Note: From any score 14-16, Alice draws 1-3 points, reaching 17-19. All outcomes ≤21, so probability is 1.0.

Constraints

  • 0 ≤ k ≤ n ≤ 104
  • 1 ≤ maxPts ≤ 104

Visualization

Tap to expand
New 21 Game: Alice's Probability Game0StartDrawing PhaseScore < k (17)Game EndsScore ≥ k (17)Check ResultScore ≤ n (21)?Draw 1-10Score ≥ 17Final checkExample: n=21, k=17, maxPts=10Alice draws until reaching 17-26 pointsWinning scores: 17, 18, 19, 20, 21 (probability ≈ 0.733)Start: Score 0Drawing: Score < kStop: Score ≥ kWin: Score ≤ n
Understanding the Visualization
1
Game Rules
Alice draws 1-maxPts points while score < k
2
Stop Condition
Game ends when score ≥ k
3
Win Condition
Calculate P(final score ≤ n)
Key Takeaway
🎯 Key Insight: Use dynamic programming to calculate probability distribution, optimizing with sliding window for O(n + k) complexity
Asked in
Google 15 Amazon 8 Microsoft 6
28.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen