Problem Description
Alice starts with 0 points and keeps drawing numbers from 1 to maxPts uniformly at random until she reaches at least k points. Once she reaches k or more points, she stops drawing. The problem asks for the probability that Alice ends up with n or fewer points.
Key Insights
- If k is 0, Alice never draws a card, so the answer is 1.
- If n is large enough (n >= k - 1 + maxPts), Alice will always win.
- Use dynamic programming to compute the probability of reaching every score from 0 to n.
- Use a sliding window sum to efficiently compute the probability recurrence relation.
- The state transition relates dp[x] to the sum of probabilities dp[x - 1] ... dp[x - maxPts] divided by maxPts.
Space and Time Complexity
Time Complexity: O(n), because we calculate each dp state at most once. Space Complexity: O(n), for storing the dp array.
Solution
We solve this problem using dynamic programming with a sliding window approach:
- Create an array dp where dp[i] represents the probability of reaching exactly i points.
- Base case: dp[0] = 1.0 (starting with 0 points).
- Use a sliding window sum to maintain the sum of dp values for states from i - maxPts to i - 1.
- For each score i from 1 to n:
- The probability dp[i] is the average of the probabilities in the window.
- If i is less than k, add dp[i] to the window sum.
- If i - maxPts is within range, subtract dp[i - maxPts] from the window sum.
- The final answer is the sum of dp[i] for i from k to n, as these are the terminal states where Alice stops.