当前位置:网站首页>60. points of N dice
60. points of N dice
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 60. n The number of dice
Ideas : Dynamic programming
hypothesis n-1 The solution of dice is f(n-1), Now add a dice ,n The sum of dice points is x Probability f(n,x) by
f ( n , x ) = ∑ i = 1 6 f ( n − 1 , x − i ) × 1 6 f(n,x)=\sum\limits_{i=1}^6f(n-1,x-i)\times\frac{1}{6} f(n,x)=i=1∑6f(n−1,x−i)×61
If calculated according to this formula , There will be an array out of bounds problem .
therefore , Consider by f(n-1,x) deduction f(n-1,x+i)
class Solution {
public:
vector<double> dicesProbability(int n) {
vector<double> dp(6,1.0/6.0);
for(int i=2;i<=n;++i){
vector<double> tmp(5*i+1,0);
for(int j=0;j<dp.size();++j){
for(int k=0;k<6;++k){
tmp[j+k]+=dp[j]/6.0;
}
}
dp=tmp;
}
return dp;
}
};
Time complexity O( n 2 n^2 n2) The calculated number of cycles is 6 × 6 , 11 × 6 , [ 5 ( n − 1 ) + 1 ] × 6 6\times6,11\times6,[5(n-1)+1]\times6 6×6,11×6,[5(n−1)+1]×6 The overall time complexity is O ( ( 6 + 5 ( n − 1 ) + 1 ) × ( n − 1 ) / 2 ) O((6+5(n-1)+1)\times(n-1)/2) O((6+5(n−1)+1)×(n−1)/2), namely O( n 2 n^2 n2)
Spatial complexity O(n) need tmp Auxiliary array
边栏推荐
- Ray. Tune visual adjustment super parameter tensorflow 2.0
- Surface net radiation flux data, solar radiation data, rainfall data, air temperature data, sunshine duration, water vapor pressure distribution, wind speed and direction data, surface temperature
- Qinglong wool - Kaka
- Introduction to MMS memory optimization of Hisilicon MPP service
- Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
- Lvgl8.1 hi3536c platform use
- Codec of ASoC framework driven by alsa
- [backtracking based on bit operation] queen n problem 2
- 2022 "college entrance examination memory" has been packaged, please check!
- Main business objects of pupanvr record (5)
猜你喜欢

Multi thread learning III. classification of threads

cellular automaton

Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)

Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)

Ecosystem type distribution data, land use data, vegetation type distribution and nature reserve distribution data

20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song

Enhanced vegetation index evi, NDVI data, NPP data, GPP data, land use data, vegetation type data, rainfall data

Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind

When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path
随机推荐
1008 color classification
MySQL Linux Installation mysql-5.7.24
Parallelization of accelerated training tf data. Dataset generator
Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ already exists; SQL statement:
JS controls the display and hiding of tags through class
Platform of ASoC framework driven by alsa
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e
Some problems of silly girl solved
C asynchronous programming (async and await) and asynchronous method synchronous invocation
Ten trends of Internet Security in 2022 industry released
LabVIEW about TDMS and Binary Storage Speed
Longest palindrome string
Qs100 at command mqtt access thingsboard
CentOS compiling and installing mysql8.0
Index fund summary
Pytorch was reported by a large number of netizens that torchrec, a new library, was "born" and has a large scale
Object class not ended
Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution
Nbiot module me3616 at command mqtt connecting thingsboard
National land use data of 30m precision secondary classification