Skip to content

Commit 347aa8c

Browse files
authored
Merge pull request neetcode-gh#2907 from mdmzfzl/main
Create: 0837-new-21-game.c
2 parents b6e7f66 + 6bc0999 commit 347aa8c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

c/0837-new-21-game.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
double new21Game(int n, int k, int maxPts) {
2+
if (k == 0 || n >= k + maxPts) {
3+
return 1.0;
4+
}
5+
6+
double windowSum = 1.0;
7+
double probability = 0.0;
8+
9+
double dp[n + 1];
10+
dp[0] = 1.0;
11+
12+
for (int i = 1; i <= n; i++) {
13+
dp[i] = windowSum / maxPts;
14+
15+
if (i < k) {
16+
windowSum += dp[i];
17+
} else {
18+
probability += dp[i];
19+
}
20+
21+
if (i >= maxPts) {
22+
windowSum -= dp[i - maxPts];
23+
}
24+
}
25+
26+
return probability;
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy