python每日一题【剑指 Offer 60. n 个骰子的点数】

day20-2022.11.15

题目信息来源
作者:Krahets
链接:https://leetcode.cn/leetbook/read/illustration-of-algorithm
来源:力扣(LeetCode)

剑指 Offer 60. n 个骰子的点数

把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。

你需要用一个浮点数数组返回答案,其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。

输入: 1
输出: [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667]
示例 2:

输入: 2
输出: [0.02778,0.05556,0.08333,0.11111,0.13889,0.16667,0.13889,0.11111,0.08333,0.05556,0.02778]

题解

这个题,呜呜呜,菜鸡不会,复现的题解,题解倒是看懂了。

链接:https://leetcode.cn/leetbook/read/illustration-of-algorithm/ozsdss/

class Solution:
    def dicesProbability(self, n: int) -> List[float]:
        dp = [1/6]*6
        for i in range(2, n+1):
            tmp = [0]*(5*i+1)
            for j in range(len(dp)):
                for k in range(6):
                    tmp[j+k] = tmp[j+k]+dp[j]/6
            dp = tmp
        return dp

你可能感兴趣的:(leetcode_python,算法,贪心算法,动态规划,leetcode,python)