Skip to content

Commit 81add0e

Browse files
committed
feat: add solutions to lc problem: No.1227
No.1227.Airplane Seat Assignment Probability
1 parent 2604173 commit 81add0e

File tree

6 files changed

+157
-2
lines changed

6 files changed

+157
-2
lines changed

solution/1200-1299/1227.Airplane Seat Assignment Probability/README.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,129 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:数学**
52+
53+
用 $f(n)$ 表示当有 $n$ 位乘客登机时,第 $n$ 位乘客坐在自己的座位上的概率。从最简单的情况开始考虑:
54+
55+
- 当 $n=1$ 时,只有 $1$ 位乘客和 $1$ 个座位,因此第 $1$ 位乘客只能坐在第 $1$ 个座位上,$f(1)=1$;
56+
57+
- 当 $n=2$ 时,有 $2$ 个座位,每个座位有 $0.5$ 的概率被第 $1$ 位乘客选中,当第 $1$ 位乘客选中座位之后,第 $2$ 位乘客只能选择剩下的座位,因此第 $2$ 位乘客有 $0.5$ 的概率坐在自己的座位上,$f(2)=0.5$。
58+
59+
当 $n>2$ 时,如何计算 $f(n)$ 的值?考虑第 $1$ 位乘客选择的座位,有以下三种情况。
60+
61+
- 第 $1$ 位乘客有 $\frac{1}{n}$ 的概率选择第 $1$ 个座位,则所有乘客都可以坐在自己的座位上,此时第 $n$ 位乘客坐在自己的座位上的概率是 $1.0$。
62+
63+
- 第 $1$ 位乘客有 $\frac{1}{n}$ 的概率选择第 $n$ 个座位,则第 $2$ 位乘客到第 $n-1$ 位乘客都可以坐在自己的座位上,第 $n$ 位乘客只能坐在第 $1$ 个座位上,此时第 $n$ 位乘客坐在自己的座位上的概率是 $0.0$。
64+
65+
- 第 $1$ 位乘客有 $\frac{n-2}{n}$ 的概率选择其余的座位,每个座位被选中的概率是 $\frac{1}{n}$。
66+
假设第 $1$ 位乘客选择第 $i$ 个座位,其中 $2 \le i \le n-1$,则第 $2$ 位乘客到第 $i-1$ 位乘客都可以坐在自己的座位上,第 $i$ 位乘客到第 $n$ 位乘客的座位不确定,第 $i$ 位乘客会在剩下的 $n-(i-1)=n-i+1$ 个座位中随机选择(包括第 $1$ 个座位和第 $i+1$ 个座位到第 $n$ 个座位)。由于此时剩下的乘客数和座位数都是 $n-i+1$,有 $1$ 位乘客会随机选择座位,因此问题规模从 $n$ 减小至 $n-i+1$。
67+
68+
结合上述三种情况,可以得到 $f(n)$ 的递推式:
69+
70+
$$
71+
\begin{aligned}
72+
f(n) &= \frac{1}{n} \times 1.0 + \frac{1}{n} \times 0.0 + \frac{1}{n} \times \sum_{i=2}^{n-1} f(n-i+1) \\
73+
&= \frac{1}{n}(1.0+\sum_{i=2}^{n-1} f(n-i+1))
74+
\end{aligned}
75+
$$
76+
77+
上述递推式中,$i$ 的取值个数有 $n-2$ 个,由于 $i$ 的取值个数必须是非负整数,因此只有当 $n-2 \ge 0$ 即 $n \ge 2$ 时,上述递推式才成立。
78+
79+
如果直接利用上述递推式计算 $f(n)$ 的值,则时间复杂度为 $O(n^2)$,无法通过全部测试用例,因此需要优化。
80+
81+
将上述递推式中的 $n$ 换成 $n-1$,可以得到递推式:
82+
83+
$$
84+
f(n-1) = \frac{1}{n-1}(1.0+\sum_{i=2}^{n-2} f(n-i))
85+
$$
86+
87+
上述递推式中,$i$ 的取值个数有 $n-3$ 个,只有当 $n-3 \ge 0$ 即 $n \ge 3$ 时,上述递推式才成立。
88+
89+
当 $n \ge 3$ 时,上述两个递推式可以写成如下形式:
90+
91+
$$
92+
\begin{aligned}
93+
n \times f(n) &= 1.0+\sum_{i=2}^{n-1} f(n-i+1) \\
94+
(n-1) \times f(n-1) &= 1.0+\sum_{i=2}^{n-2} f(n-i)
95+
\end{aligned}
96+
$$
97+
98+
将上述两式相减:
99+
100+
$$
101+
\begin{aligned}
102+
&~~~~~ n \times f(n) - (n-1) \times f(n-1) \\
103+
&= (1.0+\sum_{i=2}^{n-1} f(n-i+1)) - (1.0+\sum_{i=2}^{n-2} f(n-i)) \\
104+
&= \sum_{i=2}^{n-1} f(n-i+1) - \sum_{i=2}^{n-2} f(n-i) \\
105+
&= f(2)+f(3)+...+f(n-1) - (f(2)+f(3)+...+f(n-2)) \\
106+
&= f(n-1)
107+
\end{aligned}
108+
$$
109+
110+
整理后得到简化的递推式:
111+
112+
$$
113+
\begin{aligned}
114+
n \times f(n) &= n \times f(n-1) \\
115+
f(n) &= f(n-1)
116+
\end{aligned}
117+
$$
118+
119+
由于已知 $f(1)=1$ 和 $f(2)=0.5$,因此当 $n \ge 3$ 时,根据 $f(n) = f(n-1)$ 可知,对任意正整数 $n$ 都有 $f(n)=0.5$。又由于 $f(2)=0.5$,因此当 $n \ge 2$ 时,对任意正整数 $n$ 都有 $f(n)=0.5$。
120+
121+
由此可以得到 $f(n)$ 的结果:
122+
123+
$$
124+
f(n) = \begin{cases}
125+
1.0, & n = 1 \\
126+
0.5, & n \ge 2
127+
\end{cases}
128+
$$
129+
51130
<!-- tabs:start -->
52131

53132
### **Python3**
54133

55134
<!-- 这里可写当前语言的特殊实现逻辑 -->
56135

57136
```python
58-
137+
class Solution:
138+
def nthPersonGetsNthSeat(self, n: int) -> float:
139+
return 1 if n == 1 else .5
59140
```
60141

61142
### **Java**
62143

63144
<!-- 这里可写当前语言的特殊实现逻辑 -->
64145

65146
```java
147+
class Solution {
148+
public double nthPersonGetsNthSeat(int n) {
149+
return n == 1 ? 1 : .5;
150+
}
151+
}
152+
```
153+
154+
### **C++**
155+
156+
```cpp
157+
class Solution {
158+
public:
159+
double nthPersonGetsNthSeat(int n) {
160+
return n == 1 ? 1 : .5;
161+
}
162+
};
163+
```
164+
165+
### **Go**
66166
167+
```go
168+
func nthPersonGetsNthSeat(n int) float64 {
169+
if n == 1 {
170+
return 1
171+
}
172+
return .5
173+
}
67174
```
68175

69176
### **...**

solution/1200-1299/1227.Airplane Seat Assignment Probability/README_EN.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,41 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def nthPersonGetsNthSeat(self, n: int) -> float:
48+
return 1 if n == 1 else .5
4749
```
4850

4951
### **Java**
5052

5153
```java
54+
class Solution {
55+
public double nthPersonGetsNthSeat(int n) {
56+
return n == 1 ? 1 : .5;
57+
}
58+
}
59+
```
60+
61+
### **C++**
62+
63+
```cpp
64+
class Solution {
65+
public:
66+
double nthPersonGetsNthSeat(int n) {
67+
return n == 1 ? 1 : .5;
68+
}
69+
};
70+
```
71+
72+
### **Go**
5273
74+
```go
75+
func nthPersonGetsNthSeat(n int) float64 {
76+
if n == 1 {
77+
return 1
78+
}
79+
return .5
80+
}
5381
```
5482

5583
### **...**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
double nthPersonGetsNthSeat(int n) {
4+
return n == 1 ? 1 : .5;
5+
}
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func nthPersonGetsNthSeat(n int) float64 {
2+
if n == 1 {
3+
return 1
4+
}
5+
return .5
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public double nthPersonGetsNthSeat(int n) {
3+
return n == 1 ? 1 : .5;
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def nthPersonGetsNthSeat(self, n: int) -> float:
3+
return 1 if n == 1 else .5

0 commit comments

Comments
 (0)