867. New 21 Game
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the probability that Alice's final score is at most n. Alice draws numbers from [1, maxPts] and adds them to her score, as long as her score is less than k. She stops when her score is k or greater.
Solution
New 21 Game in Python
class Solution:
def new21Game(self, n: int, k: int, maxPts: int) -> float:
# If k is 0, Alice doesn't draw any cards and starts with 0 points.
# The score 0 is always <= n (since 0 <= k <= n), so probability is 1.
if k == 0:
return 1.0
# Optimization: If n is large enough to encompass all possible outcomes, prob is 1.
# Max score before stopping is k-1. Max draw is maxPts. Max final score is k-1+maxPts.
if n >= k - 1 + maxPts:
return 1.0
# dp[i] is the probability of having score i.
dp = [0.0] * (n + 1)
dp[0] = 1.0
# W is the sliding window sum of probabilities of scores we can draw from.
# The scores we can draw from are in the range [i - maxPts, i - 1],
# and also must be < k.
window_sum = 1.0 # Initially, only dp[0] is in the window.
result = 0.0
for i in range(1, n + 1):
# Calculate probability of reaching score i.
dp[i] = window_sum / maxPts
if i < k:
# If score i is less than k, it's a non-terminal state.
# We can draw another card from this state.
# So, add its probability to the window sum for future calculations.
window_sum += dp[i]
else:
# If score i is >= k, it's a terminal state. The game stops.
# We add its probability to our final result.
result += dp[i]
# As we move to i+1, the window slides.
# If i is large enough, dp[i - maxPts] falls out of the window.
if i >= maxPts:
window_sum -= dp[i - maxPts]
return result
New 21 Game in Java
class Solution {
public double new21Game(int n, int k, int maxPts) {
// If k is 0, Alice doesn't draw any cards and starts with 0 points.
// The score 0 is always <= n (since 0 <= k <= n), so probability is 1.
// If n >= k + maxPts - 1, Alice is guaranteed to have a score <= n.
// Max score she can get before stopping is (k-1) + maxPts.
// If n is at least this large, any outcome is valid. Sum of all probabilities is 1.
if (k == 0 || n >= k + maxPts - 1) {
return 1.0;
}
// dp[i] is the probability of having score i.
double[] dp = new double[n + 1];
dp[0] = 1.0;
// W is the sliding window sum of probabilities of scores we can draw from.
// The scores we can draw from are in the range [i - maxPts, i - 1],
// and also must be < k.
double windowSum = 1.0; // Initially, only dp[0] is in the window.
double result = 0.0;
for (int i = 1; i <= n; i++) {
// Calculate probability of reaching score i.
dp[i] = windowSum / maxPts;
if (i < k) {
// If score i is less than k, it's a non-terminal state.
// We can draw another card from this state.
// So, add its probability to the window sum for future calculations.
windowSum += dp[i];
} else {
// If score i is >= k, it's a terminal state. The game stops.
// We add its probability to our final result.
result += dp[i];
}
// As we move to i+1, the window slides.
// If i is large enough, dp[i - maxPts] falls out of the window.
if (i >= maxPts) {
windowSum -= dp[i - maxPts];
}
}
return result;
}
}
New 21 Game in C++
class Solution {
public:
double new21Game(int n, int k, int maxPts) {
// If k is 0, Alice doesn't draw any cards and starts with 0 points.
// The score 0 is always <= n (since 0 <= k <= n), so probability is 1.
if (k == 0) {
return 1.0;
}
// dp[i] is the probability of having score i.
// We need space up to n, so size n+1.
std::vector<double> dp(n + 1, 0.0);
dp[0] = 1.0;
// W is the sliding window sum of probabilities of scores we can draw from.
// The scores we can draw from are in the range [i - maxPts, i - 1],
// and also must be < k.
double windowSum = 1.0; // Initially, only dp[0] is in the window.
double result = 0.0;
for (int i = 1; i <= n; ++i) {
// Calculate probability of reaching score i.
// It's the sum of probabilities of previous valid scores, divided by maxPts.
dp[i] = windowSum / maxPts;
if (i < k) {
// If score i is less than k, it's a non-terminal state.
// We can draw another card from this state.
// So, add its probability to the window sum for future calculations.
windowSum += dp[i];
} else {
// If score i is >= k, it's a terminal state. The game stops.
// We add its probability to our final result.
// The result is the sum of probabilities of scores from k to n.
result += dp[i];
}
// As we move to i+1, the window slides.
// If i is large enough, dp[i - maxPts] falls out of the window.
// The window is for scores we can reach i+1 from, i.e., [i+1-maxPts, i].
// So, for score i, the window was [i-maxPts, i-1].
// When moving to i+1, we remove dp[i-maxPts] from the sum.
if (i >= maxPts) {
windowSum -= dp[i - maxPts];
}
}
return result;
}
};
New 21 Game in TypeScript
function new21Game(n: number, k: number, maxPts: number): number {
// If k is 0, Alice doesn't draw any cards and starts with 0 points.
// The score 0 is always <= n (since 0 <= k <= n), so probability is 1.
if (k === 0) {
return 1.0;
}
// Optimization: If n is large enough to encompass all possible outcomes, prob is 1.
// Max score before stopping is k-1. Max draw is maxPts. Max final score is k-1+maxPts.
if (n >= k - 1 + maxPts) {
return 1.0;
}
// dp[i] is the probability of having score i.
const dp: number[] = new Array(n + 1).fill(0);
dp[0] = 1.0;
// W is the sliding window sum of probabilities of scores we can draw from.
// The scores we can draw from are in the range [i - maxPts, i - 1],
// and also must be < k.
let windowSum = 1.0; // Initially, only dp[0] is in the window.
let result = 0.0;
for (let i = 1; i <= n; i++) {
// Calculate probability of reaching score i.
dp[i] = windowSum / maxPts;
if (i < k) {
// If score i is less than k, it's a non-terminal state.
// We can draw another card from this state.
// So, add its probability to the window sum for future calculations.
windowSum += dp[i];
} else {
// If score i is >= k, it's a terminal state. The game stops.
// We add its probability to our final result.
result += dp[i];
}
// As we move to i+1, the window slides.
// If i is large enough, dp[i - maxPts] falls out of the window.
if (i >= maxPts) {
windowSum -= dp[i - maxPts];
}
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.