LintCode——筛子求和
描述:扔n个骰子,向上面的数字之和为 S 。给定 Given n,请列出所有可能的 S 值及其相应的概率。
样例:给定n=1,返回 [ [1, 0.17], [2, 0.17], [3, 0.17], [4, 0.17], [5, 0.17], [6, 0.17]]
解题思路:假定有n个骰子,那么可投掷出的数字在n~6n之间,即S的取指在n~6n之间,n个骰子投掷会出现6的n次方种情况;我们采用递归思想,求n个骰子投出num的情况数的时,需要递归求n-1个骰子投出num-1,num-2,num-3...num-6的情况,而求n-1个骰子投出num-1的情况数的时候,需要递归求n-2个骰子投出num-2,num-3,num-4...,num-12的情况;依次类推,其中会有重复求解的过程,为了优化算法,我们采用动态规划技术来减少不必要的重复求解过程。使用HashMap<string,double>来存储已经计算好的情况数。这里String作为键,用以标记“x个骰子投出y”,值Double为情况数。
public class Solution {
/**
* @param n an integer
* @return a list of Map.Entry<sum, probability>
*/
public List<Map.Entry<Integer, Double>> dicesSum(int n) {
// Write your code here
// Ps. new AbstractMap.SimpleEntry<Integer, Double>(sum, pro)
// to create the pair
List<Map.Entry<Integer, Double>> list = new ArrayList<Map.Entry<Integer,Double>>(5 * n + 1);
Double pro = 0.0;
double total = Math.pow(6, n);
double conditions = 0;
HashMap<String,Double> hashMap = new HashMap<>();
for(int i = n;i <= 6 * n;i++){
pro = 0.0;
conditions = findCombs(i, n,hashMap);
pro = conditions / total;
list.add(new AbstractMap.SimpleEntry<Integer, Double>(i,pro));
}
return list;
}
public static double findCombs(int num,int n,HashMap<String, Double>hashMap){
//为了简化计算,假设每个骰子最小值为0,最大值为5,共n个骰子
double total = 0;
String key = String.valueOf(num) + "," + String.valueOf(n);
if(hashMap.containsKey(key)){//若前面已经计算过该状态则直接返回结果
return hashMap.get(key);
}
if(num <= 0){
if(n >= 1)
total = 0;
else
total = 1;//0 个骰子得到0 是可以的
}
else{
//num >0
if(n < 1){
total = 0;
}
else if(n == 1){
if(num > 6)
total = 0;
else
total = 1;
}
else{
int ceil = num <= 6 ? num : 6;
for(int i = 1;i <= ceil;i++){
total += findCombs(num-i, n-1,hashMap);
}
}
}
hashMap.put(key, total);
return total;
}
}
LintCode——筛子求和的更多相关文章
- lintcode 链表求和
题目要求 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. 样例 给出两个链表 3- ...
- lintcode循环数组之连续子数组求和
v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...
- lintcode:Add Binary 二进制求和
题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...
- Lintcode - 20.骰子求和
题目: 扔 n 个骰子,向上面的数字之和为 S.给定 Given n,请列出所有可能的 S值及其相应的概率. 给定 n = 1,返回 [ [1, 0.17], [2, 0.17], [3, 0.17] ...
- 二进制求和(LintCode)
二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 细节出了好多问题,提交了好多次... public class Solution { / ...
- 【LintCode】链表求和
问题分析: 我们通过遍历两个链表拿到每个位的值,两个值加上前一位进位值(0或者1)模10就是该位的值,除以10就是向高位的进位值(0或者1). 由于两个链表可以不一样长,所以要及时判断,一旦为null ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- lintcode-402-连续子数组求和
[402-连续子数组求和(http://www.lintcode.com/zh-cn/problem/continuous-subarray-sum/) 给定一个整数数组,请找出一个连续子数组,使得该 ...
随机推荐
- Year 2038 problem (2038年问题)
From Wikipedia, the free encyclopedia Animation showing how the date would reset, represented ...
- Linux uptime命令详解
常见的命令展示 uptime 08:21:34 up 36 min, 2 users, load average: 0.00, 0.00, 0.00 #当前服务器时间: 08:21:34 #当前服务器 ...
- MySQL案列之主从复制出错问题以及pt-slave-restart工具的使用
今天主从复制遇到一个问题,主库上插入了几百行万数据,后来又删除了这些数据,原因就是主库删除的表从库中不存在,导致从库在遇到删除不存在表的错误无法继续同步. MySQL [(none)]> sho ...
- 1001.A+B Format(10)
1001.A+B Format(20) github链接:[example link](https://github.com/wgc12/object-oriented 1.对题目的理解: 首先这道题 ...
- 10.Solr4.10.3数据导入(DIH全量增量同步Mysql数据)
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.创建MySQL数据 create database solr; use solr; DROP TABLE ...
- 关于 X509Certificate2 找到文件路径的问题
由于微信退款功能需要用到证书,当调用 X509Certificate2 的时候,会提示找不到文件而报错. X509Certificate2 cert = new X509Certificate2(文件 ...
- git回答整理
1.git常用命令 首先明确:git有工作区.暂存区.版本库,工作区是电脑里能看到的目录 创建仓库: git init newrepo,使用我们指定目录作为Git仓库(初始化后,会在newrepo目录 ...
- OpenCV——Harr特征
- rabbitmq安装错误集
1.安装依赖yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC unixODBC- ...
- openJDK环境搭建编译(fedora)
1.安装VMware VMware-workstation-full-10.0.7-2844087.exe 破解码:HY06L-F334P-9Z6H9-6R2XM-23C6J 安装完成之后, ...