本问题对应的 leetcode 原文链接:剑指 Offer 60. n个骰子的点数

问题描述

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

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

示例 1:

输入: 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]

限制:

  • 1 <= n <= 11

解题思路

视频讲解直达: 本题视频讲解

代码实现

class Solution {
    public double[] dicesProbability(int n) {
        int[][] dp = new int[n+1][6*n+1];

        for(int i = 1; i <= 6; i++){
            dp[1][i] = 1;
        }

        for(int i = 2; i <= n; i++){
            for(int j = i; j <= 6 * i; j++){
                for(int k = 1; k <= 6; k++){
                    if(j < k) break;
                    dp[i][j] += dp[i-1][j-k];
                }
            }
        }

        double[] res = new double[5*n + 1];
        int index = 0;
        double sum = Math.pow(6, n);

        for(int i = n; i <= 6 * n; i++){
            res[index++] = dp[n][i] / sum;
        }

        return res;
    }
}

时间复杂度:O(n^2)
空间复杂度:O(n)

Python

class Solution(object):
    def dicesProbability(self, n):
        """
        :type n: int
        :rtype: List[float]
        """
        dp = [[0]*(6*n+1) for _ in range(n+1)]

        for i in range(1, 7):
            dp[1][i] = 1

        for i in range(2, n+1):
            for j in range(i, 6*i+1):
                for k in range(1, 7):
                    if j < k:
                        break
                    dp[i][j] += dp[i-1][j-k]

        res = [0]*(5*n+1)
        index = 0
        s = pow(6, n)

        for i in range(n, 6*n+1):
            res[index] = float(dp[n][i]) / s
            index += 1

        return res

C++

class Solution {
public:
    vector<double> dicesProbability(int n) {
        vector<vector<int>> dp(n+1, vector<int>(6*n+1));

        for (int i = 1; i <= 6; i++) {
            dp[1][i] = 1;
        }

        for (int i = 2; i <= n; i++) {
            for (int j = i; j <= 6 * i; j++) {
                for (int k = 1; k <= 6; k++) {
                    if (j < k) break;
                    dp[i][j] += dp[i-1][j-k];
                }
            }
        }

        vector<double> res(5*n + 1);
        int index = 0;
        double sum = pow(6, n);

        for (int i = n; i <= 6 * n; i++) {
            res[index++] = static_cast<double>(dp[n][i]) / sum;
        }

        return res;
    }
};

Go

func dicesProbability(n int) []float64 {
    dp := make([][]int, n+1)
    for i := range dp {
        dp[i] = make([]int, 6*n+1)
    }

    for i := 1; i <= 6; i++ {
        dp[1][i] = 1
    }

    for i := 2; i <= n; i++ {
        for j := i; j <= 6*i; j++ {
            for k := 1; k <= 6; k++ {
                if j < k {
                    break
                }
                dp[i][j] += dp[i-1][j-k]
            }
        }
    }

    res := make([]float64, 0, 5*n+1)
    s := math.Pow(6, float64(n))

    for i := n; i <= 6*n; i++ {
        res = append(res, float64(dp[n][i])/s)
    }

    return res
}

JS

/**
 * @param {number} n
 * @return {number[]}
 */
var dicesProbability = function(n) {
    const dp = new Array(n+1).fill().map(() => new Array(6*n+1).fill(0));

    for (let i = 1; i <= 6; i++) {
        dp[1][i] = 1;
    }

    for (let i = 2; i <= n; i++) {
        for (let j = i; j <= 6*i; j++) {
            for (let k = 1; k <= 6; k++) {
                if (j < k) break;
                dp[i][j] += dp[i-1][j-k];
            }
        }
    }

    const res = new Array(5*n + 1).fill(0);
    let index = 0;
    const sum = Math.pow(6, n);

    for (let i = n; i <= 6 * n; i++) {
        res[index++] = dp[n][i] / sum;
    }

    return res;
};

发表回复

后才能评论