You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 New 21 Game 🔥 || 2 Solutions || Simple Fast and Easy || with Explanation
Solution - 1
classSolution {
doublenew21Game(int n, int k, int maxPts) {
// Corner casesif (k ==0) {
return1.0;
}
if (n >= k -1+ maxPts) {
return1.0;
}
// dp[i] is the probability of getting point i.List<double> dp =List<double>.filled(n +1, 0.0);
double probability =0.0; // dp of N or less points.double windowSum =1.0; // Sliding required window sum
dp[0] =1.0;
for (int i =1; i <= n; i++) {
dp[i] = windowSum / maxPts.toDouble();
if (i < k) {
windowSum += dp[i];
} else {
probability += dp[i];
}
if (i - maxPts >=0) {
windowSum -= dp[i - maxPts];
}
}
return probability;
}
}
Solution - 2
import'dart:math';
classCircularBuffer {
lateList<double> data;
lateint mask;
CircularBuffer(int minSize) {
final size =1<<bitLen(minSize);
mask = size -1;
data =List<double>.filled(size, 0.0);
}
doubleoperator [](int index) {
return data[index & mask];
}
voidoperator []=(int index, double value) {
data[index & mask] = value;
}
intcapacity() {
return data.length;
}
staticintbitLen(int minSize) {
return32- minSize.bitLength;
}
}
classSolution {
doublenew21Game(int n, int k, int maxPts) {
if (k ==0|| n - k +1>= maxPts) {
return1;
}
final kFactor =1.0/ maxPts;
if (maxPts +1>= n) {
return ((pow(1+ kFactor, k -1) / maxPts * (n - k +1)) *1e6).round() /1e6;
}
final dp =CircularBuffer(maxPts +1);
dp[0] =1;
var sum =1.0;
for (int i =1; i < k; ++i) {
dp[i] = sum * kFactor;
sum += dp[i] - dp[i - maxPts];
}
double answer =0.0;
for (int i = k; i <= n; ++i) {
answer += sum * kFactor;
sum -= dp[i - maxPts];
}
return (answer *1e6).round() /1e6;
}
}