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

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

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

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

示例 1:

输入: 1
输出: [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667]

思路:
使用动态规划

 public double[] dicesProbability(int n) {
         int[][] dp = new int[n+1][6*n+1];// when we have n dices, the maxSum we could get is 6*n
 // dp[i][j] means #methods of using i dices to obtain sum j

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

         for(int i=2;i<=n;i++){
             // now, we have i dices available
             for(int j=i;j<=6*i;j++){
                 // when sum is j...
                 // the current dice could take value of 1,2,...,6
                 for(int k=1;k<=6 && k<j;k++){
                     dp[i][j]+=dp[i-1][j-k];
                 }
             }
         }

         double[] ans = new double[5*n+1];// compute the final solution
         for(int j=n;j<=6*n;j++){
             ans[j-n]=(double)dp[n][j]/Math.pow(6,n);
         }
         return ans;
     }

考虑到上述二维的动态规划表dp[i][j]仅依赖于上一行dp[i-1][…], 可以做压缩, 也就是把使用两个数组交替着完成二维表dp的更新,直到更新出dp表的最后一行.
下面的代码参考了力扣K神的代码,只是为了便于理解,我做了小小的改动,也就是
dp[j]表示前i-1个骰子和为j时的概率.
tmp[j]表示前i个骰子和为j时的概率;

public double[] dicesProbability(int n) {
		// 前一个骰子和的概率分布
        double[] dp = new double[6+1];
        Arrays.fill(dp,1.0/6.0);
        dp[0]=0;
        
        for(int i=2;i<=n;i++){//轮到第i个骰子参与加和
            double[] tmp = new double[6*i+1];// 前i个骰子和的分布概率
            for(int j=1;j<dp.length;j++){
                for(int k=1;k<=6;k++){// 第i个骰子取值为k的概率为1/6
                    tmp[j+k]+=dp[j]/6.0;
                }
            }
            dp=tmp;
        }
		
		//从dp中整理结果
		// 此时dp的前一段是无用的,如当n=3时dp[0..2]是无用的,因为三个骰子的最小和为3.
        double[] res = new double[5*n+1];
        int index=0;
        for(double i:dp){
            if(i!=0)break;
            index++;
        }// 找到dp中第一个不为零的值的位置index,然后开始誊写到res
        int i=0;
        for(;index<dp.length;index++){
            res[i++]=dp[index];
        }
        return res;

    }

你可能感兴趣的:(数据结构与算法,算法)