【LeetCode】808. Soup Servings 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/soup-servings/description/
题目描述:
There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There are four kinds of operations:
- Serve 100 ml of soup A and 0 ml of soup B
- Serve 75 ml of soup A and 25 ml of soup B
- Serve 50 ml of soup A and 50 ml of soup B
- Serve 25 ml of soup A and 75 ml of soup B
When we serve some soup, we give it to someone and we no longer have it. Each turn, we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as we can. We stop once we no longer have some quantity of both types of soup.
Note that we do not have the operation where all 100 ml’s of soup B are used first.
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time.
Example:
Input: N = 50
Output: 0.625
Explanation:
If we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Notes:
- 0 <= N <= 10^9.
- Answers within 10^-6 of the true value will be accepted as correct.
题目大意
有A,B两种汤。初始每种汤各有N毫升,现有4种操作:
1. A倒出100ml,B倒出0ml
2. A倒出75ml, B倒出25ml
3. A倒出50ml, B倒出50ml
4. A倒出25ml, B倒出75ml
每种操作的概率均等为0.25。如果汤的剩余容量不足完成某次操作,则有多少倒多少。当每一种汤都倒完时停止操作。
求A先倒完的概率,加上A和B同时倒完的概率*0.5。
解题方法
这个题是个简单的记忆化搜索问题。
使用solve(A, B)函数表示当A, B分别是两者的数量的时候,A先倒完的概率,加上A和B同时倒完的概率*0.5。同时使用memo来保存这个结果。
if A <= 0 and B > 0: return 1 // A先倒完,结果是1
if A <= 0 and B <= 0: return 0.5 // A和B同时倒完,结果是题目设定的0.5
if A > 0 and B <= 0: return 0 // B先倒完,结果是0
由于四个操作发生的概率是相等的,所以,当A,B同时剩余的时候,其结果是4个操作获得概率的平均数。
另外就是题目给了提示,B没有每次倒100的情况,所以,A先倒完的概率更大。当N很大的时候,我们会做很多次操作,最后肯定是A先结束。题目要求小数点后6位,所以当N > 5600 直接 return 1.0。
另外在测试中发现,如果把(A,B)是否在记忆化数组中放到所有判断的前面,速度会加快。
时间复杂度是O(N2),空间复杂度是O(N2).
代码如下:
class Solution:
def soupServings(self, N):
"""
:type N: int
:rtype: float
"""
self.memo = dict()
if N > 5600: return 1.0
return self.solve(N, N)
def solve(self, A, B):
if (A, B) in self.memo:
return self.memo[(A, B)]
if A <= 0 and B > 0: return 1
if A <= 0 and B <= 0: return 0.5
if A > 0 and B <= 0: return 0
prob = 0.25 * (self.solve(A - 100, B) + self.solve(A - 75, B - 25)
+ self.solve(A - 50, B - 50) + self.solve(A - 25, B - 75))
self.memo[(A, B)] = prob
return prob
参考资料:
http://bookshadow.com/weblog/2018/04/02/leetcode-soup-servings/
https://leetcode.com/problems/soup-servings/discuss/121711/C%2B%2BJavaPython-When-N-greater-4800-just-return-1/185112
日期
2018 年 10 月 10 日 ———— 冻成狗
【LeetCode】808. Soup Servings 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议
Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议 索引 索引是一种存储引擎快速查询记录的一种数据结构. 注意 MYSQL一次查询只能使用一个索引 ...
- PPT插件——iSlide全功能解析
做幻灯展示是我们日常工作中不可缺少的一部分,有的人喜欢用代码如Latex, markdown+pandoc+revealjs 或 bookdown.这些都是自动化做幻灯的好工具.我也都有过体会,确实简 ...
- Demo03找素数
package Deom1;import java.awt.*;import java.util.Scanner;public class lx {//输入任意两个正整数,求出这两个正整数之间素数的个 ...
- 27.0 linux VM虚拟机IP问题
我的虚拟机是每次换一个不同的网络,b不同的ip,使用桥接模式就无法连接,就需要重新还原默认设置才行: 第一步:点击虚拟机中的编辑-->虚拟网络编辑器 第二步:点击更改设置以管理员权限进入 第三步 ...
- 一起手写吧!call、apply、bind!
apply,call,bind都是js给函数内置的一些api,调用他们可以为函数指定this的执行,同时也可以传参. call call 接收多个参数,第一个为函数上下文也就是this,后边参数为函数 ...
- k8s之ansible安装
项目地址:https://github.com/easzlab/kubeasz #:先配置harbor #:利用脚本安装docker root@k8s-harbor1:~# vim docker_in ...
- go channel 概述 - 管道
概述 unix/linux OS 的一个进程的输出可以是另一个进程的输入,这些进程使用stdin与stdout设备作为通道,在进程之间传递数据. 同样的,GO中有io.Reader与io.Writer ...
- new Date()与setDate()参数
New Date()与setDate()参数 相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结: new Date() new Date() 一共有六种形式,五种带参数 ...
- shell脚本 系统状态信息查看
一.简介 源码地址 日期:2018/6/23 介绍:显示简单的系统信息 效果图: 二.使用 适用:centos6+,ubuntu12+ 语言:中文 注意:无 下载 wget https://raw.g ...
- ORA-31633:unable to create master table "DP.SYS_EXPORT_FULL_11" ORA-01658
问题描述:在进行数据泵进行数据库备份的时候,但是导出命令报错,环境是19C 4节点的rac 一体机.目前磁盘空间需要清理,清理之前先备份一下数据库 ORA-31626:job does not exi ...