当前位置:网站首页>Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]
Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]
2022-06-12 06:36:00 【Review of the white speed Dragon King】

analysis
The brute force solution will obviously time out
And then simply a1 + … + an = k(1 <= ai <= 6)(n <= k <= 6n) My math skills can't solve
therefore We turn to dp
dp(n, x) Express n A sieve and yes x Of , Set to t
that , It will contribute to the next level , So the next floor dp(n + 1, x + i) i belongs to [1, 6] Your contribution will add t / 6
/6 It is inevitable , Because each additional layer will have one more sieve , Just one more /6
The contribution is t Can continue dp(n, x) Of course, the difference between their sum must be 1 To 6 among
dp(n + 1, x + i) = dp(n, x) Add an arbitrary sieve So just multiply 1/6
ac code
class Solution:
def dicesProbability(self, n: int) -> List[float]:
# level dp( It's a contribution )
dp = [1 / 6] * 6
for i in range(2, n + 1):
tmp = [0] * (5 * i + 1) # (6i - i + 1)
for j in range(len(dp)):
for k in range(6):
tmp[j + k] += dp[j] / 6
# update
dp = tmp
return dp
summary
For the first time dp
The result of the next layer is obtained by accumulating the contribution value
边栏推荐
- Mastering UI development with unity
- About session Getattribute, getattribute error
- Delete the duplicate items in the ordered array -- force deduction question 26 (simple)
- SQL注入原理即sqli-labs搭建,sql注入简单实战
- leetcode:剑指 Offer 67. 把字符串转换成整数【模拟 + 分割 +讨论】
- Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces
- 张驰课堂:2022年CAQ中质协六西格玛考试时间通知
- LeetCode-1741. Find total time spent per employee
- Video fire detection based on Gaussian mixture model and multi-color
- The first principle of thinking method
猜你喜欢

Use ms17-010 Eternal Blue vulnerability to infiltrate win7 and establish a permanent back door

Leetcode January 12 daily question 334 Increasing ternary subsequence

Video fire detection based on Gaussian mixture model and multi-color

leetcode:剑指 Offer 66. 构建乘积数组【前后缀积的应用】

platform driver

SQL language

数据库语法相关问题,求解一个正确语法

An error occurred while downloading the remote file The errormessage

Codeforces Round #793 (Div. 2) A B C

Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
随机推荐
Video based fire smoke detection using robust AdaBoost
Codeforces Round #793 (Div. 2) A B C
SQL 注入-盲注
Dlib face detection
VSCode常用插件
Leetcode January 12 daily question 334 Increasing ternary subsequence
SQL language
LeetCode-419. Battleship on deck
Redis data structure (VIII) -- Geo
PHP development environment construction and database addition, deletion, modification and query
六月集训 第五天——双指针
Redis application (I) -- distributed lock
PHP 开发环境搭建及数据库增删改查
(14)Blender源码分析之闪屏窗口显示软件版本号
Multithreading (V) -- Concurrent tools (II) -- j.u.c concurrent contracting (I) -- AQS and reentrantlock principles
Mastering UI development with unity
LeetCode-219. Duplicate Element II present
Set [list] to find out the subscript of repeated elements in the list (display the position of the subscript)
LeetCode-1490. Clone n-ary tree
ConVIRT论文详解(医疗图片)