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:

  • 0 <= N <= 10^9.
  • Answers within 10^-6 of the true value will be accepted as correct.

这道题给了我们两种汤,A和B,开始时各给了N毫升的。然后说是有下面四种操作:

1. 供应100毫升A汤,0毫升B汤。

2. 供应75毫升A汤,25毫升B汤。

3. 供应50毫升A汤,50毫升B汤。

4. 供应25毫升A汤,75毫升B汤。

我们选择每种操作的概率是一样的,让我们返回A汤先供应完的概率加上A汤和B汤同时供应完的一半概率。又给了一个例子来帮助我们理解。说实话,博主觉得这道题挺让人费解的,反正博主是没有啥思路,是直接研究答案的,现在就照着大神们的帖子来讲一讲吧。

先来看这四种操作,由于概率相同,所以每一种操作都的有,所以这四种操作可以想象成迷宫遍历的周围四个方向,那么我们就可以用递归来做。再看一下题目中给的N的范围,可以到10的9次方,而每次汤的消耗最多不过100毫升,由于纯递归基本就是暴力搜索,所以我们需要加上记忆数组memo,来避免重复运算,提高运行的效率。既然用记忆数组,我们不想占用太多空间,可以对工件进行优化。怎么优化呢,我们发现汤的供应量都是25的倍数,所以我们可以将25毫升当作一份汤的量,所以这四种操作就变成了:

1. 供应4份A汤,0份B汤。

2. 供应3份A汤,1份B汤。

3. 供应2份A汤,2份B汤。

4. 供应1份A汤,3份B汤。

所以我们的汤份数就是可以通过除以25来获得,由于N可能不是25的倍数,会有余数,但是就算不到一份的量,也算是完成了一个操作,所以我们可以直接加上24再除以25就可以得到正确的份数。那么接下来就是调用递归了,其实递归函数很直接了当,首先判断如果两种汤都没了,那么返回0.5,因为题目中说了如果两种汤都供应完了,返回一半的概率;如果A汤没了,返回1;如果B汤没了,返回0;如果上面的情况都没有进入,说明此时A汤和B汤都有剩余,所以我们先查记忆数组memo,如果其大于0,说明当前情况已经被计算过了,我们直接返回该值即可。如果没有的话,我们就要计算这种情况的值,通过对四种情况分别调用递归函数中,将返回的概率值累加后除以4即可。这道题还有一个很大的优化,就是当N大过某一个数值的时候,返回的都是1。这里的4800就是这个阈值返回,这样的话memo数组的大小就可以是200x200了,至于是怎么提前设定的,博主就不知道了,估计是强行试出来的吧,参见代码如下:

解法一:

class Solution {
public:
double memo[][];
double soupServings(int N) {
return N >= ? 1.0 : f((N + ) / , (N + ) / );
}
double f(int a, int b) {
if (a <= && b <= ) return 0.5;
if (a <= ) return 1.0;
if (b <= ) return ;
if (memo[a][b] > ) return memo[a][b];
memo[a][b] = 0.25 * (f(a - , b) + f(a - , b - ) + f(a - , b - ) + f(a - , b - ));
return memo[a][b];
}
};

下面这种解法的思路基本一样,就是没有用二维数组,而是用了一个HashMap来保存计算过的值,建立字符串到double到映射,这里的字符串是由A汤和B汤的剩余量拼成的,为了保证唯一性,将二者的值先转为字符串,然后在中间加一个冒号拼在一起。由于是字符串,所以我们也不用将毫升数变成份数,直接就原样保存吧,参见代码如下:

解法二:

class Solution {
public:
unordered_map<string, double> m;
double soupServings(int N) {
return N >= ? 1.0 : f(N, N);
}
double f(int a, int b) {
if (a <= && b <= ) return 0.5;
if (a <= ) return 1.0;
if (b <= ) return ;
string spoon = to_string(a) + ":" + to_string(b);
if (!m.count(spoon)) {
m[spoon] = 0.25 * (f(a - , b) + f(a - , b - ) + f(a - , b - ) + f(a - , b - ));
}
return m[spoon];
}
};

参考资料:

https://leetcode.com/problems/soup-servings/discuss/125809/Java-soup-(spoon-included)

https://leetcode.com/problems/soup-servings/discuss/121711/C++JavaPython-When-N-greater-4800-just-return-1

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Soup Servings 供应汤的更多相关文章

  1. [Swift]LeetCode808. 分汤 | Soup Servings

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...

  2. 【LeetCode】808. Soup Servings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/soup-serv ...

  3. Weekly Contest 78-------->808. Soup Servings

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...

  4. 解码问题--leetcode:91 (2019商汤笔试)

    题目:有一种将字母编码成数字的方式:'a'->1, 'b->2', ... , 'z->26'. 现在给一串数字,给出有多少种可能的译码结果. 想法: 该题就是动态规划问题,建议在写 ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  7. [LeetCode] Largest Plus Sign 最大的加型符号

    In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...

  8. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. LFYZ-OJ ID: 1010 天使的起誓

    思路 理解题目后,会发现是一个高精度除低精度求余问题,非常简单. 容易出错的地方是:求余结果为0的时候,此时,天使所在的盘子号码其实就是n,如果直接返回余数,得到的结果则是0. 被除数的范围是2-10 ...

  2. vue-resource的使用,前后端数据交互

    vue-resource的使用,前后端数据交互 1:导入vue与vue-resource的js js下载:   https://pan.baidu.com/s/1fs5QaNwcl2AMEyp_kUg ...

  3. 第九节:JWT简介和以JS+WebApi为例基于JWT的安全校验

    一. 简介 1. 背景 传统的基于Session的校验存在诸多问题,比如:Session过期.服务器开销过大.不能分布式部署.不适合前后端分离的项目. 传统的基于Token的校验需要存储Key-Val ...

  4. ArcGis 属性表.dbf文件使用Excel打开中文乱码的解决方法

    2019年4月 拓展: ArcGis——好好的属性表,咋就乱码了呢? 2019年3月27日补充: 在ArcMap10.3+(根据官网描述应该是,作者测试使用10.5,可行)以后的版本,可以使用ArcT ...

  5. 安装tftp

    #!/bin/bash # tftp install # 20180711 # 仅测试过操作系统 ubuntu 16.04 download_url='http://img.fe.okjiaoyu.c ...

  6. Java 多线程总结

    昨天熬了个通宵,看了一晚上的视频,把java 的多线程相关技术重新复习了一遍,下面对学习过程中遇到的知识点进行下总结. 首先我们先来了解一下进程.线程.并发执行的概念: 进程是指:一个内存中运行的应用 ...

  7. 论文笔记:Deep feature learning with relative distance comparison for person re-identification

    这篇论文是要解决 person re-identification 的问题.所谓 person re-identification,指的是在不同的场景下识别同一个人(如下图所示).这里的难点是,由于不 ...

  8. c++ 智能指针用法详解

    本文介绍c++里面的四个智能指针: auto_ptr, shared_ptr, weak_ptr, unique_ptr 其中后三个是c++11支持,并且第一个已经被c++11弃用. 为什么要使用智能 ...

  9. Beta 冲刺(4/7)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(4/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 整理博客 ppt模板 接下来的计划 做好机动. ...

  10. rsync3.1.3的编译安装和常用操作

    .rsync的编译安装 .tar.gz cd rsync- ./configure --prefix=/usr/local/rsync- --disable-ipv6 .rsync的配置文件: [ro ...