作者: 负雪明烛
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. java的缓冲流及使用Properties集合存取数据(遍历,store,load)

    缓冲流 概述 字节缓冲流:BufferedInputStream,BufferedOutputStream 字符缓冲流:BufferedReader,BufferedWriter 缓冲流原理 缓冲区是 ...

  2. 阿里云ECS磁盘性能测试

    阿里官方给出的性能指标 顺序读 测试命令 fio -directory=/var/lib/data -direct=1 -iodepth=1 -thread -ioengine=libaio -ran ...

  3. 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用

    7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...

  4. C/C++ Qt 数据库与TreeView组件绑定

    在上一篇博文<C/C++ Qt 数据库QSql增删改查组件应用>介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数 ...

  5. Hadoop、Hive【LZO压缩配置和使用】

    目录 一.编译 二.相关配置 三.为LZO文件创建索引 四.Hive为LZO文件建立索引 1.hive创建的lzo压缩的分区表 2.给.lzo压缩文件建立索引index 3.读取Lzo文件的注意事项( ...

  6. 编程之美Q1

    题目 和数书页有点类似,就直接数吧 #include<iostream> using namespace std; class q1 { public: size_t func(size_ ...

  7. Stream.toMap

    Collectors类的tomap方法将流收集到映射实例中. list 转 map collection.stream().collect(Collectors.toMap(User::getId, ...

  8. Linux基础命令---htdigest建立和更新apache服务器摘要

    htdigest htdigest指令用来建立和更新apache服务器用于摘要认证的存放用户认证信息的文件. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.   1.语法   ...

  9. GET传参数方式

    controller:/getDetail/{id} /getDetail?id1234567 /getDetail?id=id1234567

  10. 【C/C++】BanGDream活动点数计算器

    作为一个白嫖咸鱼,我每个活动都只打出三星卡就不玩了,于是写了一个模拟器,算算还要打几把hhh #include <iostream> #include <algorithm> ...