Skip to content

Commit

Permalink
Create 25 May | 837. New 21 Game.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SumitPareek2401 authored May 27, 2023
1 parent 990e9cc commit 596413e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 25 May | 837. New 21 Game.cpp
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;
}
};

0 comments on commit 596413e

Please sign in to comment.