-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_score_game.go
33 lines (31 loc) · 916 Bytes
/
card_score_game.go
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
package problem0837
/*
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.
*/
func new21Game(n int, k int, maxPts int) float64 {
var res, wsum float64 = 0, 1
var dp []float64
if k == 0 || n >= k+maxPts {
return 1
}
dp = make([]float64, n+1)
dp[0] = 1
for i := 1; i <= n; i++ {
dp[i] = wsum / float64(maxPts)
if i < k {
wsum += dp[i]
} else {
res += dp[i]
}
if i-maxPts >= 0 {
wsum -= dp[i-maxPts]
}
}
return res
}