-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path837.cpp
More file actions
24 lines (24 loc) · 746 Bytes
/
837.cpp
File metadata and controls
24 lines (24 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
double new21Game(int n, int k, int maxPts) {
// dp[i]: win prob when has sum = i card on hand
vector<double> dp(k + maxPts, 0.0);
double sum = 0;
// after k -1 (or start from k)
// alice cannot get new card, so she win or lose
// if sum <= n she win => prob = 1
// else she lose => prob = 0
for (int i = k; i < k + maxPts; ++i) {
if (i <= n) dp[i] = 1;
else dp[i] = 0;
sum += dp[i];
}
// prob. of getting new card is equal
for (int i = k - 1; i >= 0; --i) {
dp[i] = sum / maxPts;
sum += dp[i];
sum -= dp[i + maxPts];
}
return dp[0];
}
};