-
Notifications
You must be signed in to change notification settings - Fork 5
/
new_21_game.dart
118 lines (77 loc) · 2.18 KB
/
new_21_game.dart
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
- New 21 Game -
Alice plays the following game, loosely based on the card game "21".
Alice starts with 0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where maxPts is an integer. Each draw is independent and the outcomes have equal probabilities.
Alice stops drawing numbers when she gets k or more points.
Return the probability that Alice has n or fewer points.
Answers within 10-5 of the actual answer are considered accepted.
Example 1:
Input: n = 10, k = 1, maxPts = 10
Output: 1.00000
Explanation: Alice gets a single card, then stops.
Example 2:
Input: n = 6, k = 1, maxPts = 10
Output: 0.60000
Explanation: Alice gets a single card, then stops.
In 6 out of 10 possibilities, she is at or below 6 points.
Example 3:
Input: n = 21, k = 17, maxPts = 10
Output: 0.73278
Constraints:
0 <= k <= n <= 104
1 <= maxPts <= 104
*/
// class Solution {
// double new21Game(int n, int k, int maxPts) {
// return 300.0;
// }
// }
import 'dart:math';
class CircularBuffer {
late List<double> data;
late int mask;
CircularBuffer(int minSize) {
final size = 1 << bitLen(minSize);
mask = size - 1;
data = List<double>.filled(size, 0.0);
}
double operator [](int index) {
return data[index & mask];
}
void operator []=(int index, double value) {
data[index & mask] = value;
}
int capacity() {
return data.length;
}
static int bitLen(int minSize) {
return 32 - minSize.bitLength;
}
}
class Solution {
double new21Game(int n, int k, int maxPts) {
if (k == 0 || n - k + 1 >= maxPts) {
return 1;
}
final kFactor = 1.0 / maxPts;
if (maxPts + 1 >= n) {
return ((pow(1 + kFactor, k - 1) / maxPts * (n - k + 1)) * 1e6).round() / 1e6;
}
final dp = CircularBuffer(maxPts + 1);
dp[0] = 1;
var sum = 1.0;
for (int i = 1; i < k; ++i) {
dp[i] = sum * kFactor;
sum += dp[i] - dp[i - maxPts];
}
double answer = 0.0;
for (int i = k; i <= n; ++i) {
answer += sum * kFactor;
sum -= dp[i - maxPts];
}
return (answer * 1e6).round() / 1e6;
}
}
class A {
var a =9;
}