牛客-剑指offer系列题解:n个骰子的点数

1、问题描述:

牛客-剑指offer系列题解:n个骰子的点数_第1张图片
2、数据结构:

数组
3、题解:
方法1:哈希表
由于n的范围较小,可以把所有的结果都考虑一下输出即可。

class Solution:
    def twoSum(self, n: int) -> List[float]:
        if n == 1:
            return [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667]
        elif n == 2:
            return [0.02778,0.05556,0.08333,0.11111,0.13889,0.16667,0.13889,0.11111,0.08333,0.05556,0.02778]
        elif n == 3:
            return [0.00463,0.01389,0.02778,0.04630,0.06944,0.09722,0.11574,0.12500,0.12500,0.11574,0.09722,0.06944,0.04630,0.02778,0.01389,0.00463]
        elif n == 4:
            return [0.00077,0.00309,0.00772,0.01543,0.02701,0.04321,0.06173,0.08025,0.09645,0.10802,0.11265,0.10802,0.09645,0.08025,0.06173,0.04321,0.02701,0.01543,0.00772,0.00309,0.00077]
        elif n == 5:
            return [0.00013,0.00064,0.00193,0.00450,0.00900,0.01620,0.02636,0.03922,0.05401,0.06944,0.08372,0.09452,0.10031,0.10031,0.09452,0.08372,0.06944,0.05401,0.03922,0.02636,0.01620,0.00900,0.00450,0.00193,0.00064,0.00013]
        elif n == 6:
            return [0.00002,0.00013,0.00045,0.00120,0.00270,0.00540,0.00977,0.01620,0.02488,0.03571,0.04816,0.06121,0.07354,0.08372,0.09047,0.09285,0.09047,0.08372,0.07354,0.06121,0.04816,0.03571,0.02488,0.01620,0.00977,0.00540,0.00270,0.00120,0.00045,0.00013,0.00002]
        elif n == 7:
            return [0.00000,0.00003,0.00010,0.00030,0.00075,0.00165,0.00328,0.00595,0.01003,0.01578,0.02336,0.03266,0.04328,0.05454,0.06547,0.07499,0.08204,0.08579,0.08579,0.08204,0.07499,0.06547,0.05454,0.04328,0.03266,0.02336,0.01578,0.01003,0.00595,0.00328,0.00165,0.00075,0.00030,0.00010,0.00003,0.00000]
        elif n == 8:
            return [0.00000,0.00000,0.00002,0.00007,0.00020,0.00047,0.00102,0.00201,0.00366,0.00624,0.01001,0.01517,0.02184,0.02994,0.03918,0.04905,0.05883,0.06769,0.07477,0.07936,0.08094,0.07936,0.07477,0.06769,0.05883,0.04905,0.03918,0.02994,0.02184,0.01517,0.01001,0.00624,0.00366,0.00201,0.00102,0.00047,0.00020,0.00007,0.00002,0.00000,0.00000]
        elif n == 9:
            return [0.00000,0.00000,0.00000,0.00002,0.00005,0.00013,0.00030,0.00063,0.00124,0.00226,0.00390,0.00635,0.00982,0.01448,0.02040,0.02753,0.03567,0.04442,0.05324,0.06148,0.06844,0.07349,0.07615,0.07615,0.07349,0.06844,0.06148,0.05324,0.04442,0.03567,0.02753,0.02040,0.01448,0.00982,0.00635,0.00390,0.00226,0.00124,0.00063,0.00030,0.00013,0.00005,0.00002,0.00000,0.00000,0.00000]
        elif n == 10:
            return [0.00000,0.00000,0.00000,0.00000,0.00001,0.00003,0.00008,0.00019,0.00039,0.00077,0.00141,0.00245,0.00403,0.00634,0.00954,0.01375,0.01904,0.02539,0.03262,0.04046,0.04846,0.05612,0.06287,0.06816,0.07153,0.07269,0.07153,0.06816,0.06287,0.05612,0.04846,0.04046,0.03262,0.02539,0.01904,0.01375,0.00954,0.00634,0.00403,0.00245,0.00141,0.00077,0.00039,0.00019,0.00008,0.00003,0.00001,0.00000,0.00000,0.00000,0.00000]
        elif n == 11:
            return [0.00000,0.00000,0.00000,0.00000,0.00000,0.00001,0.00002,0.00005,0.00012,0.00025,0.00048,0.00088,0.00154,0.00257,0.00409,0.00625,0.00919,0.01301,0.01778,0.02347,0.02995,0.03702,0.04432,0.05145,0.05793,0.06331,0.06715,0.06916,0.06916,0.06715,0.06331,0.05793,0.05145,0.04432,0.03702,0.02995,0.02347,0.01778,0.01301,0.00919,0.00625,0.00409,0.00257,0.00154,0.00088,0.00048,0.00025,0.00012,0.00005,0.00002,0.00001,0.00000,0.00000,0.00000,0.00000,0.00000]

方法2:动态规划

class Solution:
    def twoSum(self, n: int) -> List[float]:
        dp = [0 for _ in range(6*n+1)]#索引0不取,后面取到最大索引6*n

        for i in range(1,7):#初始化do,第一轮的抛掷
            dp[i]=1
        for i in range(2,n+1):#从第二轮抛掷开始算
            for j in range(6*n,i-1,-1):#第二轮抛掷最小和为2,从大到小更新对应               ##的抛掷次数
                dp[j]=0#每次投掷要从0更新dp[j]大小,点数和出现的次数要重新计算
                for cur in range(1,7):#每次抛掷的点数
                    if j -cur < i - 1:break#意思为上一轮的最小点数为i-1
                    dp[j]+=dp[j-cur]#根据上一轮来更新当前轮数据
        sum_ = 6**n#一共抛掷了6**n次
        res = []
        for i in range(n,6*n+1):
            res.append(dp[i]*1.0/sum_)#最终计算结果
        return res 

4、复杂度分析:

方法1:
时间复杂度都为O(1)
空间复杂度都为O(6*n),n为骰子的数量

你可能感兴趣的:(牛客-剑指offer系列题解)