当前位置:网站首页>[brush title] probability of winning a draw
[brush title] probability of winning a draw
2022-06-12 13:10:00 【m0_ sixty million six hundred and thirty-one thousand three hun】
One 、 subject
Google interview questions
Will face value 1-N Your cards form a group
Each time from the group with medium probability 1-N One of them
Next time, I'll change to a new group , There are infinite groups
When the drawn cards add up <a when , Will draw all the time
When you add up and >=a And <b when , You will win
When you add up and >=b when , You will fail
Given parameters N,a,b Returns the probability of winning
Two 、 Answer key
The core idea 
2.1 recursive
public static double f2(int N,int a,int b){
if(N < 1 || a >= b || a < 0){
return 0.0;
}
if(b-a>=N){
return 1.0;
}
return p2(0,N,a,b);
}
//cur Indicates the current cumulative sum
public static double p2(int cur,int N,int a,int b){
if(cur>=a&&cur<b){
return 1.0;
}
if(cur>=b){
return 0.0;
}
double w=0.0;
for (int i = 1; i <=N ; i++) {
w+=p2(cur+i,N,a,b);
}
return w/N;
}
2.2 Optimal recursive
Optimize... In recursive functions for loop , Because the cumulative sum is calculated as cur The probability of winning , Need to pass through for Loop enumeration , What we want to do is optimize the loop process 
public static double f3(int N,int a,int b){
if(N < 1 || a >= b || a < 0){
return 0.0;
}
if(b-a>=N){
return 1.0;
}
return p3(0,N,a,b);
}
public static double p3(int cur, int N,int a,int b){
if(cur>=a&&cur<b){
return 1.0;
}
if(cur>=b){
return 0.0;
}
if(cur==a-1){
return 1.0*(b-a)/N;
}
double w=p3(cur+1,N,a,b)+p3(cur+1,N,a,b)*N;
if(cur+1+N<b){
w-=p3(cur+N+1,N,a,b);
}
return w/N;
}
2.3 Dynamic programming
take 2.2 Code can be changed to dynamic planning
public static double f4(int N,int a,int b){
if(N < 1 || a >= b || a < 0){
return 0.0;
}
if(b-a>=N){
return 1.0;
}
double[] dp=new double[b];
for (int i = a; i <b ; i++) {
dp[i]=1.0;
}
if(a-1>=0){
dp[a-1]=1.0*(b-a)/N;
}
for (int cur = a-2; cur >=0 ; cur--) {
double w = dp[cur + 1] + dp[cur + 1] * N;
if (cur + 1 + N < b) {
w -= dp[cur + 1 + N];
}
dp[cur] = w / N;
}
return dp[0];
}
边栏推荐
- Structure matérielle du système embarqué - introduction du Conseil de développement embarqué basé sur arm
- 【刷题篇】超级洗衣机
- 嵌入式系统概述2-嵌入式系统组成和应用
- 【云原生 | Kubernetes篇】深入了解Deployment(八)
- 苹果电脑上MySQL安装完成找不到怎么办
- Constant time delete / find any element in array
- It is enough to read this article. Web Chinese development
- Pytoch official fast r-cnn source code analysis (I) -- feature extraction
- hudi 键的生成(Key Generation)
- Embedded driver design
猜你喜欢

Overview of embedded system 1- definition, characteristics and development history of embedded system

Semantic segmentation with pytorch

Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s

镜像扫描工具预研

Design virtual network to realize communication between virtual machine instance and external network

import torch_geometric 的Data 查看

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

Newoj week 10 question solution

Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题

嵌入式系统硬件构成-基于ARM的嵌入式开发板介绍
随机推荐
Successful job hopping Ali, advanced learning
[wechat applet development] Part 1: development tool installation and program configuration
苹果电脑上MySQL安装完成找不到怎么办
import torch_ Data view of geometric
Structure matérielle du système embarqué - introduction du Conseil de développement embarqué basé sur arm
Embedded system hardware composition - embedded system hardware architecture
About paiwen
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting bar plot, adding significance level and jitter points
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
How to balance multiple losses in deep learning?
NVIDIA Jetson Nano Developer Kit 入门
import torch_ Geometric loads some common datasets
嵌入式系统概述2-嵌入式系统组成和应用
C#DBHelper_FactoryDB_GetConn
Dameng database DM8 Windows environment installation
C language [23] classic interview questions [2]
verilog-mode的简要介绍
[VIM] VIM plug-in youcompleteme configuration file
Newoj week 10 question solution
Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
