๐ŸŽฒ New 21 Game - Probability Challenge

Alice is playing an exciting card game loosely based on Blackjack (21)! Here's how it works:

  • ๐ŸŽฏ Starting Point: Alice begins with 0 points
  • ๐ŸŽด Drawing Rules: She draws cards while she has less than k points
  • ๐ŸŽฒ Random Gain: Each draw gives her a random number of points from [1, maxPts] with equal probability
  • ๐Ÿ›‘ Stop Condition: She stops when she reaches k or more points

Your Mission: Calculate the probability that Alice ends up with n or fewer points when the game ends!

๐Ÿ’ก Think of it like this: If Alice needs to reach at least k=10 points to stop, and can draw 1-3 points each turn, what's the chance she doesn't go over n=15 total points?

Input & Output

example_1.py โ€” Simple Case
$ Input: n = 10, k = 1, maxPts = 10
โ€บ Output: 1.00000
๐Ÿ’ก Note: Alice draws once (since k=1) and gets 1-10 points. All outcomes (1-10) are โ‰ค n=10, so probability = 1.0
example_2.py โ€” Standard Game
$ Input: n = 6, k = 1, maxPts = 10
โ€บ Output: 0.60000
๐Ÿ’ก Note: Alice draws once and gets 1-10 points. Only outcomes 1-6 are โ‰ค n=6, so probability = 6/10 = 0.6
example_3.py โ€” Multi-Draw Game
$ Input: n = 21, k = 17, maxPts = 10
โ€บ Output: 0.73278
๐Ÿ’ก Note: Alice must reach 17+ points, then stop. Complex probability calculation considering all paths that end with โ‰ค 21 points.

Visualization

Tap to expand
New 21 Game: Probability Flow Visualization๐ŸŽฏ Game Flow: Alice draws until score โ‰ฅ k, then stopsSTARTScore: 0Prob: 1.0123Each draw: 1/maxPts probabilitykStop Drawing!ร—Score > nโœ“Score โ‰ค n๐Ÿ”‘ Key Insight: Sliding Window DPโ€ข dp[i] = probability of reaching score i while drawingโ€ข windowSum efficiently tracks sum of previous maxPts probabilitiesโ€ข Result = sum of dp[i] for all i where k โ‰ค i โ‰ค nTime: O(n) | Space: O(n) - Optimal Solution! ๐Ÿš€
Understanding the Visualization
1
Initial State
Alice starts with 100% probability at score 0
2
Probability Distribution
Each draw distributes probability equally among next maxPts positions
3
Sliding Window
Efficiently track sum of probabilities that can contribute to current state
4
Final Accumulation
Sum probabilities for all valid ending scores (k โ‰ค score โ‰ค n)
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window optimization transforms an exponential brute force solution into a linear-time algorithm by efficiently tracking probability distributions without redundant calculations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + k)

Single pass through all relevant scores with sliding window optimization

n
2n
โœ“ Linear Growth
Space Complexity
O(n + k)

DP array to store probabilities for each possible score

n
2n
โšก Linearithmic Space

Constraints

  • 0 โ‰ค k โ‰ค n โ‰ค 104
  • 1 โ‰ค maxPts โ‰ค 104
  • Answer precision: within 10-5 of actual answer
Asked in
Google 23 Amazon 18 Microsoft 12 Meta 8
42.8K Views
Medium Frequency
~25 min Avg. Time
1.5K 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