808 Soup Servings
808. Soup Servings
1. Question
There are two types of soup: type A and type B. Initially we haveN
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.
Notes:
0 <= N <= 10^9
Answers within
10^-6
of the true value will be accepted as correct.
2. Implementation
(1) DFS + Memoization
思路: 这题刚开始看不太好理解,但题目要求的是当soup A的量为0的概率 加上 soup A和soup B的量同时为0的概率的一半。仔细分析,我们可以得到结果可以分解为几个base case:
1.A和B的量都为0
2.A的量为0,B的量不为0
3.A的量不为0, B的量为0
根据题目要求可知,我们只需要得到base case 1和2即可, 其中我们设base case1的概率为1,base case 2的概率为0.5(因为题目要求这种情况下我们只要得到概率的一半), base case 3的概率为0, 由于选择4种操作的概率都为0.25,所以再选择每一个操作概率都要乘以0.25。通过画出递归树(比如N = 200),我们发现会有overlapping subproblem(比如A = 25, B = 175这个subproblem出现重复),所以需要利用记忆化搜索避免重复计算。
3. Time & Space Complexity
DFS + Memoization:时间复杂度O(n ^ 2), 对于量为N的A和B来说,总共有N ^ 2种状态,其中通过记忆化搜索,每种状态最多只被计算一次, 空间复杂度O(n ^ 2)
Last updated
Was this helpful?