Skip to content

Commit 5c5d965

Browse files
committed
leetcode
1 parent 3c2ba28 commit 5c5d965

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed

New21Game/new_21_game.dart

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
3+
- New 21 Game -
4+
5+
6+
7+
Alice plays the following game, loosely based on the card game "21".
8+
9+
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.
10+
11+
Alice stops drawing numbers when she gets k or more points.
12+
13+
Return the probability that Alice has n or fewer points.
14+
15+
Answers within 10-5 of the actual answer are considered accepted.
16+
17+
18+
19+
Example 1:
20+
21+
Input: n = 10, k = 1, maxPts = 10
22+
Output: 1.00000
23+
Explanation: Alice gets a single card, then stops.
24+
Example 2:
25+
26+
Input: n = 6, k = 1, maxPts = 10
27+
Output: 0.60000
28+
Explanation: Alice gets a single card, then stops.
29+
In 6 out of 10 possibilities, she is at or below 6 points.
30+
Example 3:
31+
32+
Input: n = 21, k = 17, maxPts = 10
33+
Output: 0.73278
34+
35+
36+
Constraints:
37+
38+
0 <= k <= n <= 104
39+
1 <= maxPts <= 104
40+
41+
42+
*/
43+
44+
45+
// class Solution {
46+
// double new21Game(int n, int k, int maxPts) {
47+
// return 300.0;
48+
// }
49+
// }
50+
51+
52+
import 'dart:math';
53+
54+
class CircularBuffer {
55+
late List<double> data;
56+
late int mask;
57+
58+
CircularBuffer(int minSize) {
59+
final size = 1 << bitLen(minSize);
60+
mask = size - 1;
61+
data = List<double>.filled(size, 0.0);
62+
}
63+
64+
double operator [](int index) {
65+
return data[index & mask];
66+
}
67+
68+
void operator []=(int index, double value) {
69+
data[index & mask] = value;
70+
}
71+
72+
int capacity() {
73+
return data.length;
74+
}
75+
76+
static int bitLen(int minSize) {
77+
return 32 - minSize.bitLength;
78+
}
79+
}
80+
81+
class Solution {
82+
double new21Game(int n, int k, int maxPts) {
83+
if (k == 0 || n - k + 1 >= maxPts) {
84+
return 1;
85+
}
86+
87+
final kFactor = 1.0 / maxPts;
88+
if (maxPts + 1 >= n) {
89+
return ((pow(1 + kFactor, k - 1) / maxPts * (n - k + 1)) * 1e6).round() / 1e6;
90+
}
91+
92+
final dp = CircularBuffer(maxPts + 1);
93+
dp[0] = 1;
94+
var sum = 1.0;
95+
96+
for (int i = 1; i < k; ++i) {
97+
dp[i] = sum * kFactor;
98+
sum += dp[i] - dp[i - maxPts];
99+
}
100+
101+
double answer = 0.0;
102+
103+
for (int i = k; i <= n; ++i) {
104+
answer += sum * kFactor;
105+
sum -= dp[i - maxPts];
106+
}
107+
108+
return (answer * 1e6).round() / 1e6;
109+
}
110+
}
111+
112+
113+
114+

New21Game/new_21_game.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
func new21Game(n, k, maxPts int) float64 {
4+
// Corner cases
5+
if k == 0 {
6+
return 1.0
7+
}
8+
if n >= k-1+maxPts {
9+
return 1.0
10+
}
11+
12+
// dp[i] is the probability of getting point i.
13+
dp := make([]float64, n+1)
14+
15+
probability := 0.0 // dp of N or less points.
16+
windowSum := 1.0 // Sliding required window sum
17+
dp[0] = 1.0
18+
19+
for i := 1; i <= n; i++ {
20+
dp[i] = windowSum / float64(maxPts)
21+
22+
if i < k {
23+
windowSum += dp[i]
24+
} else {
25+
probability += dp[i]
26+
}
27+
28+
if i-maxPts >= 0 {
29+
windowSum -= dp[i-maxPts]
30+
}
31+
}
32+
33+
return probability
34+
}
35+
36+
//=================
37+
38+
// import (
39+
// "math"
40+
// )
41+
42+
// type CircularBuffer struct {
43+
// data []float64
44+
// mask uint
45+
// }
46+
47+
// func NewCircularBuffer(minSize int) *CircularBuffer {
48+
// size := 1 << bitLen(minSize)
49+
// mask := uint(size - 1)
50+
// data := make([]float64, size)
51+
// return &CircularBuffer{
52+
// data: data,
53+
// mask: mask,
54+
// }
55+
// }
56+
57+
// func (cb *CircularBuffer) Get(index int) float64 {
58+
// return cb.data[index&int(cb.mask)]
59+
// }
60+
61+
// func (cb *CircularBuffer) Set(index int, value float64) {
62+
// cb.data[index&int(cb.mask)] = value
63+
// }
64+
65+
// func (cb *CircularBuffer) Capacity() int {
66+
// return len(cb.data)
67+
// }
68+
69+
// func bitLen(minSize int) int {
70+
// return 32 - clz(minSize)
71+
// }
72+
73+
// func clz(x int) int {
74+
// if x <= 0 {
75+
// return 32
76+
// }
77+
// return 31 - int(math.Log2(float64(x)))
78+
// }
79+
80+
// func new21Game(n, k, maxPts int) float64 {
81+
// if k == 0 || n-k+1 >= maxPts {
82+
// return 1.0
83+
// }
84+
85+
// kFactor := 1.0 / float64(maxPts)
86+
// if maxPts+1 >= n {
87+
// return math.Pow(1+kFactor, float64(k-1)) / float64(maxPts) * (float64(n-k) + 1)
88+
// }
89+
90+
// dp := NewCircularBuffer(maxPts + 1)
91+
// dp.Set(0, 1.0)
92+
// sum := 1.0
93+
94+
// for i := 1; i < k; i++ {
95+
// dp.Set(i, sum*kFactor)
96+
// sum += dp.Get(i) - dp.Get(i-maxPts)
97+
// }
98+
99+
// answer := 0.0
100+
// for i := k; i <= n; i++ {
101+
// answer += sum * kFactor
102+
// sum -= dp.Get(i - maxPts)
103+
// }
104+
105+
// return answer
106+
// }
107+
108+
// /*
109+
110+
// */

New21Game/new_21_game.md

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 🔥 New 21 Game 🔥 || 2 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
double new21Game(int n, int k, int maxPts) {
8+
// Corner cases
9+
if (k == 0) {
10+
return 1.0;
11+
}
12+
if (n >= k - 1 + maxPts) {
13+
return 1.0;
14+
}
15+
16+
// dp[i] is the probability of getting point i.
17+
List<double> dp = List<double>.filled(n + 1, 0.0);
18+
19+
double probability = 0.0; // dp of N or less points.
20+
double windowSum = 1.0; // Sliding required window sum
21+
dp[0] = 1.0;
22+
23+
for (int i = 1; i <= n; i++) {
24+
dp[i] = windowSum / maxPts.toDouble();
25+
26+
if (i < k) {
27+
windowSum += dp[i];
28+
} else {
29+
probability += dp[i];
30+
}
31+
32+
if (i - maxPts >= 0) {
33+
windowSum -= dp[i - maxPts];
34+
}
35+
}
36+
37+
return probability;
38+
}
39+
}
40+
```
41+
42+
## Solution - 2
43+
44+
```dart
45+
import 'dart:math';
46+
47+
class CircularBuffer {
48+
late List<double> data;
49+
late int mask;
50+
51+
CircularBuffer(int minSize) {
52+
final size = 1 << bitLen(minSize);
53+
mask = size - 1;
54+
data = List<double>.filled(size, 0.0);
55+
}
56+
57+
double operator [](int index) {
58+
return data[index & mask];
59+
}
60+
61+
void operator []=(int index, double value) {
62+
data[index & mask] = value;
63+
}
64+
65+
int capacity() {
66+
return data.length;
67+
}
68+
69+
static int bitLen(int minSize) {
70+
return 32 - minSize.bitLength;
71+
}
72+
}
73+
74+
class Solution {
75+
double new21Game(int n, int k, int maxPts) {
76+
if (k == 0 || n - k + 1 >= maxPts) {
77+
return 1;
78+
}
79+
80+
final kFactor = 1.0 / maxPts;
81+
if (maxPts + 1 >= n) {
82+
return ((pow(1 + kFactor, k - 1) / maxPts * (n - k + 1)) * 1e6).round() / 1e6;
83+
}
84+
85+
final dp = CircularBuffer(maxPts + 1);
86+
dp[0] = 1;
87+
var sum = 1.0;
88+
89+
for (int i = 1; i < k; ++i) {
90+
dp[i] = sum * kFactor;
91+
sum += dp[i] - dp[i - maxPts];
92+
}
93+
94+
double answer = 0.0;
95+
96+
for (int i = k; i <= n; ++i) {
97+
answer += sum * kFactor;
98+
sum -= dp[i - maxPts];
99+
}
100+
101+
return (answer * 1e6).round() / 1e6;
102+
}
103+
}
104+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
214214
- [**989.** Add to Array-Form of Integer](AddToArrayFormOfInteger/add_to_array_form_of_integer.dart)
215215
- [**1472.** Design Browser History](DesignBrowserHistory/design_browser_history.dart)
216216
- [**347.** Top K Frequent Elements](TopKFrequentElements/top_k_frequent_elements.dart)
217+
- [**837.** New 21 Game](New21Game/new_21_game.dart)
217218

218219
## Reach me via
219220

0 commit comments

Comments
 (0)