We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d6cc37f commit a7bc2c9Copy full SHA for a7bc2c9
1 file changed
c++/leetcode/new21game.cpp
@@ -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