Skip to content

Commit a7bc2c9

Browse files
authored
Create new21game.cpp
1 parent d6cc37f commit a7bc2c9

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

c++/leetcode/new21game.cpp

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

0 commit comments

Comments
 (0)