Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to calculate the points of n dice by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:05:26 09月28日 Update

This article mainly introduces LeetCode how to find n dice points, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Topic description

Throw n dice on the ground and the sum of the points on one side of all the dice is s. Enter n to print out the probability of occurrence of all possible values of s.

You need to return the answer with a floating-point array, where the I element represents the probability of the smaller I of the set of points that the n dice can throw.

1 probability dictionary. Initialize dp [0] = 1. The probability that the sum of points is 0 for 0 dice is 1.

# after adding a dice, we only need to add 1x 6 to the sum of each point in the original dictionary as the sum of the new points, and multiply the original probability by 1 stroke 6 to add to the new points and the corresponding probability

Dp = {}

Dp [0] = 1

For i in range (1, n + 1):

Newdp = collections.defaultdict (int)

For sm in dp:

For v in range (1,7):

# after adding a dice, add its probability to the new point sum

Newdp [sm + v] + = dp [sm] / 6

Dp = newdp

Res = []

For sm in range (n, 6 * n + 1):

# store the values in the result in turn

Res.append (dp[ sm])

Return res

Thank you for reading this article carefully. I hope the article "how to get the points of n dice" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Points dice probability sum dictionary article title complexity situation input and so on number code representative that is result analogy output minimum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB vpn Microsoft macOS