【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 ...
随机推荐
- 34. Swap Nodes in Pairs
Swap Nodes in Pairs My Submissions QuestionEditorial Solution Total Accepted: 95230 Total Submission ...
- Selenium-IDE,在网页上模拟人的操作
想偷懒,不想做很机械重复的网页操作,就百度了一下看看有什么方法,能把自己从重复性的网页操作中解放出来,于是,百度到了selenium ide,折腾许久,用最新版火狐添加了自带selenium ide组 ...
- 05 Windows安装python3.6.4+pycharm环境
windows安装python3.6.4环境 使用微信扫码关注微信公众号,并回复:"Python工具包",免费获取下载链接! 一.卸载python环境 卸载以下软件: 二.安装py ...
- javaSE中级篇3——集合体系(另外一种存储容器)——更新完毕
集合还是一种工具,所以它们的包都在java.util包下 1.集合的整个体系结构(是需要掌握的体系,完全体系不是这样) 对图中所说的 序和重复 这两词的说明: 序:指的是添加进去的元素和取出来的元素 ...
- Hadoop入门 集群崩溃的处理方法
目录 集群崩溃的处理方法 搞崩集群 错误示范 正确处理方法 1 回到hadoop的家目录 2 杀死进程 3 删除每个集群的data和logs 4 格式化 5 启动集群 总结 原因分析 集群崩溃的处理方 ...
- 转 Android应用开发必备的20条技能
https://blog.csdn.net/u012269126/article/details/52433237 有些andorid开发人员感觉很迷茫,接下来该去看系统源码还是继续做应用,但是感觉每 ...
- 【Linux】【Services】【SaaS】Spinnaker
1. 简介 1.1. 说明: Spinnaker 是 Netflix 的开源项目,是一个持续交付平台,它定位于将产品快速且持续的部署到多种云平台上.Spinnaker 通过将发布和各个云平台解耦,来将 ...
- Advanced C++ | Conversion Operators
In C++, the programmer abstracts real world objects using classes as concrete types. Sometimes it is ...
- RAC(Reactive Cocoa)常见的类
导入ReactiveCocoa框架 在终端,进入Reactive Cocoa文件下 创建podfile 打开该文件 并配置 use_frameworks! pod 'ReactiveCocoa', ' ...
- 【科研】科研【合同】盖章流程/横向·非涉密/电子科技大学
[前置手续] 一.在科研管理系统里填单子,立项. 二.科研管理系统审核通过后,对于对面给好的合同,在合同系统里选择[合同业务发起-发起非标准合同],填单子. 三.会有一系列的审核,审核完成后打印合同备 ...