【leetcode】1230.Toss Strange Coins
题目如下:
You have some coins. The
i
-th coin has a probabilityprob[i]
of facing heads when tossed.Return the probability that the number of coins facing heads equals
target
if you toss every coin exactly once.Example 1:
Input: prob = [0.4], target = 1
Output: 0.40000Example 2:
Input: prob = [0.5,0.5,0.5,0.5,0.5], target = 0
Output: 0.03125Constraints:
1 <= prob.length <= 1000
0 <= prob[i] <= 1
0 <= target
<= prob.length
- Answers will be accepted as correct if they are within
10^-5
of the correct answer.
解题思路:动态规划。首先假设dp[i][j]为投完第i枚硬币后有j枚硬币朝上的概率。如果第i-1枚硬币投完后有j枚朝上,那么第i枚硬币就只能朝下;如果i-1枚硬币投完后有j-1枚朝上,那么第i枚硬币就只能朝上。很容易建立状态转移方程,dp[i][j] = dp[i-1][j-1] * prob[i] + dp[i-1][j] * (1 - prob[i])。
代码如下:
class Solution(object):
def probabilityOfHeads(self, prob, target):
"""
:type prob: List[float]
:type target: int
:rtype: float
"""
dp = [[0]*(len(prob) + 1) for _ in prob] dp[0][0] = 1 - prob[0]
dp[0][1] = prob[0] for i in range(1,len(prob)):
for j in range(min(target+1,len(prob))):
if j == 0:
dp[i][j] = float(dp[i-1][j]) * (1 - prob[i])
continue
dp[i][j] = float(dp[i-1][j-1]) * prob[i] + float(dp[i-1][j]) * (1 - prob[i])
return dp[-1][target]
【leetcode】1230.Toss Strange Coins的更多相关文章
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- Golang的面向对象编程【结构体、方法、继承、接口】
Golang也支持面向对象编程.但与以前学过传统的面向对象编程语言有区别.1)Golang没有类class,Go语言的结构体struct和类class有相似的特性.2)Golang中不存在继承,方法重 ...
- linux下mongodb程序和c++客户端的编译
2016-4-6 14:17:15 安装前准备:1/ 安装boost库2/ 安装scons程序 方法一:$ git clone git://github.com/mongodb/mongo ...
- Python基础知识思维导图|自学Python指南
微信公众号[软件测试大本营]回复"python",获取50本python精华电子书. 测试/开发知识干货,互联网职场,程序员成长崛起,终身学习. 现在最火的编程语言是什么?答案就是 ...
- Alias sample(别名采样)
应用场景:加权采样,即按照随机事件出现的概率抽样 具体算法: 举例如上,随机事件出现的概率依次是1/2,1/3,1/12,1/12;记随机事件的个数为N,则所有事件概率乘以N后概率为2,4/3,1/3 ...
- Linux vim文件编辑器使用
学习目标: 通过本实验熟练vim的使用. 步骤: 1.将用户家目录的ls结果重定向到vimfile.txt 2.查看rh124第403页实验要求,并完成 参考命令: 复制文件前,需要先建立文件,教材上 ...
- 【生成树趣题】CF723F st-Spanning Tree
题目传送门 题意: 给定一个n个点m条边的无向联通图,没有重边和自环.给定s和t,求一棵生成树,使得s,t的度数不超过ds,dt.若有解,输出“Yes”和方案(多组方案输出任意一组),若无解,输出“N ...
- 【图像处理】【计算机视觉】findContours的使用
原文地址:findContours函数参数说明及相关函数作者:鸳都学童 findContours函数,这个函数的原型为: void findContours(InputOutputArray imag ...
- 关于Eclipse及JDK安装过程中的一些问题
一,环境变量的配置 1.配置CLASSPATH系统变量 CLASSPATH系统变量为类查找路径 ①.在使用javac进行编译时遇到import时候就会通过这个变量里面配置的路径去查找.如果配置的是目录 ...
- HTTPS为什么是安全的?
学习自https://www.cnblogs.com/zhangsanfeng/p/9125732.html,感谢博主 超文本传输协议HTTP被用于在web浏览器和网站服务器之间传递信息,但以明文方式 ...
- shell基础#1
shell:能直接调用命令(python)1.bash的基本特性 ctrl+L 清屏2.IO重定向与管道符 都由shell提供 命令是一个可执行的二进制程序3.编程基础 编程原理 程序:执行某个功能的 ...