"837. New 21 Game" medium, trying to solve with probability manipulation

This commit is contained in:
2022-10-09 20:06:42 +03:00
parent ad8f04f025
commit 5093a17767
2 changed files with 83 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"math" "math"
"math/rand" "math/rand"
"strconv"
"time" "time"
) )
@@ -302,7 +303,7 @@ func new21GameExperimental(n int, k int, maxPts int, rngType RngType) float64 {
makeExperiment := func() float64 { makeExperiment := func() float64 {
var sum uint64 var sum uint64
for sum = 0; sum <= uintK; sum += getNextRandom() { for sum = 0; sum < uintK; sum += getNextRandom() {
} }
kOrMoreCount++ kOrMoreCount++
@@ -333,6 +334,84 @@ func new21GameExperimental(n int, k int, maxPts int, rngType RngType) float64 {
return probability return probability
} }
func New21Game(n int, k int, maxPts int) float64 { func new21RecursiveBruteforceV2(n int, k int, p int, s int, str string) (int, int) {
return new21GameExperimental(n, k, maxPts, FastRng) moreThanK := 0
lessThanN := 0
if p <= 0 || n <= 0 || k <= 0 || s >= k {
return 0, 0
}
sum := s + p
if sum >= k {
str += strconv.Itoa(p) + "=" + strconv.Itoa(sum)
println(str)
moreThanK++
if sum <= n {
lessThanN++
}
} else {
str += strconv.Itoa(p) + "+"
for i := p; i > 0; i-- {
retMoreThanK, retLessThanN := new21RecursiveBruteforceV2(n, k, i, sum, str)
moreThanK += retMoreThanK
lessThanN += retLessThanN
}
return moreThanK, lessThanN
}
return moreThanK, lessThanN
}
func new21GameBruteforce(n int, k int, maxPts int) float64 {
moreThanK, lessThanN := 0, 0
for m := maxPts; m > 0; m-- {
retMoreThanK, retLessThanN := new21RecursiveBruteforceV2(n, k, m, 0, "")
moreThanK += retMoreThanK
lessThanN += retLessThanN
}
return float64(lessThanN) / float64(moreThanK)
}
func New21Game(n int, k int, maxPts int) float64 {
if k < 0 || n < 0 || maxPts < 0 {
return 0
}
if k == 0 {
return 1
}
if maxPts == 0 {
return 0
}
if n < k {
return 1
}
p := make([]float64, n+1)
m := float64(maxPts)
p[0] = 1
p[1] = p[0] / m
for i := 2; i <= k; i++ {
p[i] = p[i-1] + p[i-1]/m
}
for i := k + 1; i <= n; i++ {
p[i] = p[i-1] + p[i-1]/m - p[i-k]
}
sumP := float64(0)
for i := k; i <= n; i++ {
sumP += p[i]
}
return sumP
} }

View File

@@ -6,7 +6,7 @@ import (
) )
func TestNew21Game(t *testing.T) { func TestNew21Game(t *testing.T) {
// assert.Equal(t, 1.0, New21Game(10, 1, 10)) assert.Equal(t, 1.0, New21Game(10, 1, 10))
assert.Equal(t, 0.6, New21Game(6, 1, 10)) assert.Equal(t, 0.6, New21Game(6, 1, 10))
assert.Equal(t, 0.73278, New21Game(21, 17, 10)) assert.Equal(t, 0.73278, New21Game(21, 17, 10))
} }