New 21 Game - Problem
๐ฒ 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
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
โ Linear Growth
Space Complexity
O(n + k)
DP array to store probabilities for each possible score
โก Linearithmic Space
Constraints
- 0 โค k โค n โค 104
- 1 โค maxPts โค 104
- Answer precision: within 10-5 of actual answer
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code