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/) 给定一个整数数组,请找出一个连续子数组,使得该 ...
随机推荐
- sha256sum和 md5sum 命令之间的区别
Short answer: For verifying ISOs, there is no practical difference, use whichever you want, as long ...
- [Spark Core] 在 Spark 集群上运行程序
0. 说明 将 IDEA 下的项目导出为 Jar 包,部署到 Spark 集群上运行. 1. 打包程序 1.0 前提 搭建好 Spark 集群,完成代码的编写. 1.1 修改代码 [添加内容,判断参数 ...
- .NET Core 使用 EF 出错的解决方法
在.NET Core 项目钟(类库),使用Entity Framework,建立模型生成数据库时,失败 Could not load assembly 'xxx'. Ensure it is refe ...
- VS创建工程出错解决方案
今天在用VS2010创建工程时出现错误:“ 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, Pu ...
- November 09th, 2017 Week 45th Thursday
If we did all the things we are capable of, we would literally astound ourselves. 我们如果尽全力去完成我们能做到的事情 ...
- PyQt5--StatusBar
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- node+koa中转层开发实践总结
node中转层的意义: 1.能解决前后端代码部署在不同服务器下时的跨域问题.(实现) 2.合并请求,业务逻辑处理.(实现) 3.单页应用的首屏服务端渲染.(暂未实现) 环境准备: node: ^8.1 ...
- Docker技术入门与实战 第二版-学习笔记-8-网络功能network-3-容器访问控制和自定义网桥
1)容器访问控制 容器的访问控制,主要通过 Linux 上的 iptables防火墙来进行管理和实现. iptables是 Linux 上默认的防火墙软件,在大部分发行版中都自带. 容器访问外部网络 ...
- nginx静态文件缓存的解决方案
nginx的一大功能就是完成静态资源的分离部署,减轻后端服务器的压力,如果给这些静态资源再加一级nginx的缓存,可以进一步提升访问效率. 第一步:添加nginx.conf的http级别的缓存配置 # ...
- Android 网络请求超时处理方案
以用户登录为例介绍用户访问网络时的请求超时处理的两种方法: 1)使用android提供的工具类AsyncTask类,此类提供了一个AsyncTask.execute().get(timeout, un ...