Skip to content

Commit 34d7d91

Browse files
committed
✨feat: add 808
1 parent f4ac328 commit 34d7d91

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

Index/数学.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
| [780. 到达终点](https://leetcode-cn.com/problems/reaching-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reaching-points/solution/by-ac_oier-hw11/) | 困难 | 🤩🤩🤩🤩🤩 |
5252
| [789. 逃脱阻碍者](https://leetcode-cn.com/problems/escape-the-ghosts/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/escape-the-ghosts/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-w69gr/) | 中等 | 🤩🤩🤩🤩🤩 |
5353
| [793. 阶乘函数后 K 个零](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/) | [LeetCode 题解链接](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/solution/by-ac_oier-pk9g/) | 困难 | 🤩🤩🤩🤩 |
54+
| [808. 分汤](https://leetcode.cn/problems/soup-servings/) | [LeetCode 题解链接](https://leetcode.cn/problems/soup-servings/solution/by-ac_oier-3n1h/) | 中等 | 🤩🤩🤩🤩 |
5455
| [810. 黑板异或游戏](https://leetcode-cn.com/problems/chalkboard-xor-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/chalkboard-xor-game/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ges7k/) | 困难 | 🤩🤩🤩🤩 |
5556
| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 |
5657
| [829. 连续整数求和](https://leetcode.cn/problems/consecutive-numbers-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/consecutive-numbers-sum/solution/by-ac_oier-220q/) | 困难 | 🤩🤩🤩🤩 |

Index/线性 DP.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| [688. 骑士在棋盘上的概率](https://leetcode-cn.com/problems/knight-probability-in-chessboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/knight-probability-in-chessboard/solution/gong-shui-san-xie-jian-dan-qu-jian-dp-yu-st8l/) | 中等 | 🤩🤩🤩🤩🤩 |
1919
| [741. 摘樱桃](https://leetcode.cn/problems/cherry-pickup/) | [LeetCode 题解链接](https://leetcode.cn/problems/cherry-pickup/solution/by-ac_oier-pz7i/) | 困难 | 🤩🤩🤩🤩 |
2020
| [799. 香槟塔](https://leetcode.cn/problems/champagne-tower/) | [LeetCode 题解链接](https://leetcode.cn/problems/champagne-tower/solution/by-ac_oier-c8jn/) | 中等 | 🤩🤩🤩 |
21+
| [808. 分汤](https://leetcode.cn/problems/soup-servings/) | [LeetCode 题解链接](https://leetcode.cn/problems/soup-servings/solution/by-ac_oier-3n1h/) | 中等 | 🤩🤩🤩🤩 |
2122
| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 |
2223
| [978. 最长湍流子数组](https://leetcode-cn.com/problems/longest-turbulent-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-turbulent-subarray/solution/xiang-jie-dong-tai-gui-hua-ru-he-cai-dp-3spgj/) | 中等 | 🤩🤩🤩🤩 |
2324
| [1137. 第 N 个泰波那契数](https://leetcode-cn.com/problems/n-th-tribonacci-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-th-tribonacci-number/solution/gong-shui-san-xie-yi-ti-si-jie-die-dai-d-m1ie/) | 简单 | 🤩🤩🤩🤩 |
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[808. 分汤](https://leetcode.cn/problems/soup-servings/solution/by-ac_oier-3n1h/)** ,难度为 **中等**
4+
5+
Tag : 「数学」、「动态规划」
6+
7+
8+
9+
有 `A` 和 `B` 两种类型 的汤。一开始每种类型的汤有 `n` 毫升。有四种分配操作:
10+
11+
1. 提供 `100ml` 的 汤A 和 `0ml` 的 汤B 。
12+
2. 提供 `75ml` 的 汤A 和 `25ml` 的 汤B 。
13+
3. 提供 `50ml` 的 汤A 和 `50ml` 的 汤B 。
14+
4. 提供 `25ml` 的 汤A 和 `75ml` 的 汤B 。
15+
16+
当我们把汤分配给某人之后,汤就没有了。每个回合,我们将从四种概率同为 `0.25` 的操作中进行分配选择。如果汤的剩余量不足以完成某次操作,我们将尽可能分配。当两种类型的汤都分配完时,停止操作。
17+
18+
注意 不存在先分配 `100 ml` 汤B 的操作。
19+
20+
需要返回的值: 汤A 先分配完的概率 +  汤A和汤B 同时分配完的概率 / 2。返回值在正确答案 $10^{-5}$ 的范围内将被认为是正确的。
21+
22+
示例 1:
23+
```
24+
输入: n = 50
25+
26+
输出: 0.62500
27+
28+
解释:如果我们选择前两个操作,A 首先将变为空。
29+
对于第三个操作,A 和 B 会同时变为空。
30+
对于第四个操作,B 首先将变为空。
31+
所以 A 变为空的总概率加上 A 和 B 同时变为空的概率的一半是 0.25 *(1 + 1 + 0.5 + 0)= 0.625。
32+
```
33+
示例 2:
34+
```
35+
输入: n = 100
36+
37+
输出: 0.71875
38+
```
39+
40+
提示:
41+
* $0 <= n <= 10^9$
42+
43+
---
44+
45+
### 数学 + 动态规划
46+
47+
四种分配方式都是 $25$ 的倍数,因此我们可以将 $n$ 进行除以 $25$ 上取整的缩放操作,并将四类操作等价成:
48+
49+
1. 提供 `4ml` 的 汤A 和 `0ml` 的 汤B 。
50+
2. 提供 `3ml` 的 汤A 和 `1ml` 的 汤B 。
51+
3. 提供 `2ml` 的 汤A 和 `2ml` 的 汤B 。
52+
4. 提供 `1ml` 的 汤A 和 `3ml` 的 汤B 。
53+
54+
定义 $f[i][j]$ 为 汤A 剩余 $i$ 毫升,汤B 剩余 $j$ 毫升时的最终概率($概率 = 汤A先分配完的概率 + 汤A和汤B同时分配完的概率 \times 0.5$)。
55+
56+
最终答案为 $f[n][n]$ 为最终答案,考虑任意项存在为 $0$ 情况时的边界情况:
57+
58+
* 若 $i = 0$ 且 $j = 0$,结果为 $0 + \frac{1}{2} = \frac{1}{2}$,即有 $f[0][0] = 0.5$
59+
* 若 $i = 0$ 且 $j > 0$,结果为 $1 + 0 = 1$,即有 $f[0][X] = 1$,其中 $X > 1$
60+
* 若 $i > 0$ 且 $j = 0$,结果为 $0 + 0 = 0$,即有 $f[X][0] = 0$,其中 $X > 1$
61+
62+
其余一般情况为 $i$ 和 $j$ 均不为 $0$,由于四类操作均为等概率,结合题意和状态定义可知:
63+
64+
$$
65+
f[i][j] = \frac{1}{4} \times (f[i - 4][j] + f[i - 3][j - 1] + f[i - 2][j - 2] + f[i - 1][j - 3])
66+
$$
67+
68+
由于 $n = 1e9$,即使进行了除 $25$ 的缩放操作,过多的状态数仍会导致 `TLE`
69+
70+
此时需要利用「返回值在正确答案 $10^{-5}$ 的范围内将被认为是正确的」来做优化(一下子不太好想到):由于四类操作均是等概率,单个回合期望消耗汤 A 的量为 $2.5$,消耗汤 B 的量为 $1.5$。
71+
72+
因此当 $n$ 足够大,操作回合足够多,汤 A 将有较大的概率结束分配,即当 $n$ 足够大,概率值会趋向于 $1$。
73+
74+
我们考虑多大的 $n$ 能够配合精度误差 $10^{-5}$ 来减少计算量:一个可行的操作是利用上述的 DP 思路 + 二分的方式找到符合精度要求的验算值(不超过 $200$)。
75+
76+
Java 代码:
77+
```Java
78+
class Solution {
79+
public double soupServings(int n) {
80+
n = Math.min(200, (int) Math.ceil(n / 25.0));
81+
double[][] f = new double[n + 10][n + 10];
82+
f[0][0] = 0.5;
83+
for (int j = 1; j <= n; j++) f[0][j] = 1;
84+
for (int i = 1; i <= n; i++) {
85+
for (int j = 1; j <= n; j++) {
86+
double a = f[Math.max(i - 4, 0)][j], b = f[Math.max(i - 3, 0)][Math.max(j - 1, 0)];
87+
double c = f[Math.max(i - 2, 0)][Math.max(j - 2, 0)], d = f[Math.max(i - 1, 0)][Math.max(j - 3, 0)];
88+
f[i][j] = 0.25 * (a + b + c + d);
89+
}
90+
}
91+
return f[n][n];
92+
}
93+
}
94+
```
95+
Python 代码:
96+
```Python
97+
class Solution:
98+
def soupServings(self, n: int) -> float:
99+
n = min(200, math.ceil(n / 25))
100+
f = [[0] * (n + 10) for _ in range(n + 10)]
101+
f[0][0] = 0.5
102+
for j in range(1, n + 10):
103+
f[0][j] = 1
104+
for i in range(1, n + 1):
105+
for j in range(1, n + 1):
106+
a, b = f[max(i - 4, 0)][j], f[max(i - 3, 0)][max(j - 1, 0)]
107+
c, d = f[max(i - 2, 0)][max(j - 2, 0)], f[max(i - 1, 0)][max(j - 3, 0)]
108+
f[i][j] = 0.25 * (a + b + c + d)
109+
return f[n][n]
110+
```
111+
* 时间复杂度:$O(m^2)$,其中 $m = 200$ 为验算值
112+
* 空间复杂度:$O(m^2)$
113+
114+
---
115+
116+
### 最后
117+
118+
这是我们「刷穿 LeetCode」系列文章的第 `No.808` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
119+
120+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
121+
122+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
123+
124+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
125+

0 commit comments

Comments
 (0)