Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-07-29 16:35:42
https://leetcode.com/problems/soup-servings/description/
808. Soup Servings
你现在有两种汤要分给一群人,给你一个 n 表示这两种汤的 ml 数量,我们有四种分汤
的方法:
1.A分出100ml B分出0ml
2.A分出75ml B分出25ml
3.A分出50ml B分出50ml
4.A分出25ml B分出75ml
求出依照上述四种分汤的方法,A种类的汤会先被分完的机率,如果A和B同时分完则机率
是50%。
Example 1:
Input: n = 50
Output: 0.62500
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.
Example 2:
Input: n = 100
Output: 0.71875
思路:
1.对四个选择进行DFS,如果满足A先没的话机率就为1,如果同时没则机率为0.5。
2.加入一个 memo 记忆已算过的分配比率。
3.然后吃了MLE = = ,实在是不懂要怎么继续优化,看了一下其他人的解当 n 越大
的时候,A被先分配完的机率会越来越高(因为分配方式2/3对A有利),因为题目
说实际答案和你给的答案相差在 10^-5 之间可以接受 ,所以当 n 大到一个程度
的时候机率会是 0.99999xxxxxx 这个时候我们直接返回 1 就可以了,只能一个数
字一个数字跑看跑到哪个 n 之后精度可以忽略,当 n = 4450 的时候误差为:
1 - 0.9999893866772255 = 0.00001061332 ,所以当 n 高于这个数字直接返回
1 就好。
Java Code:

Links booklink

Contact Us: admin [ a t ] ucptt.com