-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew21Game_837.java
36 lines (35 loc) · 925 Bytes
/
New21Game_837.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.example;
public class New21Game_837 {
class Solution {
public double new21Game(int n, int k, int maxPts) {
if (k==0 || n>=k+maxPts-1){
return 1.0;
}
int w = maxPts;
double res = 0;
double wSum = 1;
double[] dp = new double[n+1];
dp[0] = 1;
for (int i=1;i<k;i++){
dp[i] = wSum/w;
wSum += dp[i];
if (i>=w){
wSum -= dp[i-w];
}
}
double a;
for (int i=k;i<=n;i++){
a = wSum/w;
res += a;
dp[i] = a;
if (i>=w){
wSum -= dp[i-w];
if (wSum == 0){
break;
}
}
}
return res;
}
}
}