-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 25 May | 837. New 21 Game.cpp
- Loading branch information
1 parent
990e9cc
commit 596413e
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
public: | ||
double new21Game(int n, int k, int maxPts) { | ||
if(k == 0 || n >= k+maxPts) | ||
return 1; | ||
|
||
vector<double>dp(n+1, 0.0); | ||
dp[0] = 1; | ||
double currSum = dp[0]; | ||
|
||
for(int i = 1; i <= n; i++){ | ||
dp[i] = currSum / (double)maxPts; | ||
if(i<k){ | ||
currSum += dp[i]; | ||
} | ||
if(i - maxPts >= 0){ | ||
currSum -= dp[i - maxPts]; | ||
} | ||
} | ||
double ans = 0; | ||
for(int i = k; i <= n; i++){ | ||
ans += dp[i]; | ||
} | ||
return ans; | ||
} | ||
}; |