作者: 负雪明烛
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:

  1. Serve 100 ml of soup A and 0 ml of soup B
  2. Serve 75 ml of soup A and 25 ml of soup B
  3. Serve 50 ml of soup A and 50 ml of soup B
  4. 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:

  1. 0 <= N <= 10^9.
  2. 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)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. R语言与医学统计图形-【19】ggplot2坐标轴调节

    ggplot2绘图系统--坐标轴调节 scale函数:图形遥控器.坐标轴标度函数: scale_x_continous scale_y_continous scale_x_discrete scale ...

  2. PHP socket Workerman实用的php框架

    PHP socket Workerman是一款开源高性能异步PHP socket即时通讯框架. 非常好用的一款框架,可以支持在线聊天,长连接等 用法 官方 https://www.workerman. ...

  3. CMSIS-RTOS 信号量Semaphores

    信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...

  4. 点击下拉选择触发事件【c#】

    <asp:DropDownList ID="ddlRegionList" runat="server" AutoPostBack="true&q ...

  5. 日常Java 2021/9/28

    字符串反转 package m; public class m { public static void main(String[] args) { //定义一个字符串 String str = &q ...

  6. 学习java 7.16

    学习内容: 线程安全的类 Lock锁 生产者消费者模式 Object类的等待唤醒方法 明天内容: 网络编程 通信程序 遇到问题: 无

  7. 【SpringBoot】几种定时任务的实现方式

    SpringBoot 几种定时任务的实现方式 Wan QingHua 架构之路  定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java ...

  8. 应用层协议——DHCP

    常见协议分层 网洛层协议:包括:IP协议.ICMP协议.ARP协议.RARP协议. 传输层协议:TCP协议.UDP协议. 应用层协议:FTP.Telnet.SMTP.HTTP.RIP.NFS.DNS ...

  9. 动态规划系列(零)—— 动态规划(Dynamic Programming)总结

    动态规划三要素:重叠⼦问题.最优⼦结构.状态转移⽅程. 动态规划的三个需要明确的点就是「状态」「选择」和「base case」,对应着回溯算法中走过的「路径」,当前的「选择列表」和「结束条件」. 某种 ...

  10. Win10 Chrome 在DPI缩放下导致界面放大问题 解决方案

    支持:54.0.2840.59 m (64-bit) 以下大多数版本,具体未测试.如有问题可以反馈一下. 方法1:为程序设置"高DPI设置时禁用显示缩放. 方法2:为程序添加启动参数: /h ...