|
38 | 38 |
|
39 | 39 | ## Solutions
|
40 | 40 |
|
| 41 | +**Solution 1: Mathematics** |
| 42 | + |
| 43 | +Let $f(n)$ represent the probability that the $n$th passenger will sit in their own seat when there are $n$ passengers boarding. Consider from the simplest case: |
| 44 | + |
| 45 | +- When $n=1$, there is only 1 passenger and 1 seat, so the first passenger can only sit in the first seat, $f(1)=1$; |
| 46 | + |
| 47 | +- When $n=2$, there are 2 seats, each seat has a probability of 0.5 to be chosen by the first passenger. After the first passenger chooses a seat, the second passenger can only choose the remaining seat, so the second passenger has a probability of 0.5 to sit in their own seat, $f(2)=0.5$. |
| 48 | + |
| 49 | +When $n>2$, how to calculate the value of $f(n)$? Consider the seat chosen by the first passenger, there are three cases. |
| 50 | + |
| 51 | +- The first passenger has a probability of $\frac{1}{n}$ to choose the first seat, then all passengers can sit in their own seats, so the probability of the $n$th passenger sitting in their own seat is 1.0. |
| 52 | + |
| 53 | +- The first passenger has a probability of $\frac{1}{n}$ to choose the $n$th seat, then the second to the $(n-1)$th passengers can sit in their own seats, the $n$th passenger can only sit in the first seat, so the probability of the $n$th passenger sitting in their own seat is 0.0. |
| 54 | + |
| 55 | +- The first passenger has a probability of $\frac{n-2}{n}$ to choose the remaining seats, each seat has a probability of $\frac{1}{n}$ to be chosen. |
| 56 | + Suppose the first passenger chooses the $i$th seat, where $2 \le i \le n-1$, then the second to the $(i-1)$th passengers can sit in their own seats, the seats of the $i$th to the $n$th passengers are uncertain, the $i$th passenger will randomly choose from the remaining $n-(i-1)=n-i+1$ seats (including the first seat and the $(i+1)$th to the $n$th seats). Since the number of remaining passengers and seats is $n-i+1$, and 1 passenger will randomly choose a seat, the problem size is reduced from $n$ to $n-i+1$. |
| 57 | + |
| 58 | +Combining the above three cases, we can get the recursive formula of $f(n)$: |
| 59 | + |
| 60 | +$$ |
| 61 | +\begin{aligned} |
| 62 | +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) \\ |
| 63 | +&= \frac{1}{n}(1.0+\sum_{i=2}^{n-1} f(n-i+1)) |
| 64 | +\end{aligned} |
| 65 | +$$ |
| 66 | + |
| 67 | +In the above recursive formula, there are $n-2$ values of $i$, since the number of values of $i$ must be a non-negative integer, so the above recursive formula only holds when $n-2 \ge 0$ i.e., $n \ge 2$. |
| 68 | + |
| 69 | +If you directly use the above recursive formula to calculate the value of $f(n)$, the time complexity is $O(n^2)$, which cannot pass all test cases, so it needs to be optimized. |
| 70 | + |
| 71 | +Replace $n$ with $n-1$ in the above recursive formula, you can get the recursive formula: |
| 72 | + |
| 73 | +$$ |
| 74 | +f(n-1) = \frac{1}{n-1}(1.0+\sum_{i=2}^{n-2} f(n-i)) |
| 75 | +$$ |
| 76 | + |
| 77 | +In the above recursive formula, there are $n-3$ values of $i$, and the above recursive formula only holds when $n-3 \ge 0$ i.e., $n \ge 3$. |
| 78 | + |
| 79 | +When $n \ge 3$, the above two recursive formulas can be written as follows: |
| 80 | + |
| 81 | +$$ |
| 82 | +\begin{aligned} |
| 83 | +n \times f(n) &= 1.0+\sum_{i=2}^{n-1} f(n-i+1) \\ |
| 84 | +(n-1) \times f(n-1) &= 1.0+\sum_{i=2}^{n-2} f(n-i) |
| 85 | +\end{aligned} |
| 86 | +$$ |
| 87 | + |
| 88 | +Subtract the above two formulas: |
| 89 | + |
| 90 | +$$ |
| 91 | +\begin{aligned} |
| 92 | +&~~~~~ n \times f(n) - (n-1) \times f(n-1) \\ |
| 93 | +&= (1.0+\sum_{i=2}^{n-1} f(n-i+1)) - (1.0+\sum_{i=2}^{n-2} f(n-i)) \\ |
| 94 | +&= \sum_{i=2}^{n-1} f(n-i+1) - \sum_{i=2}^{n-2} f(n-i) \\ |
| 95 | +&= f(2)+f(3)+...+f(n-1) - (f(2)+f(3)+...+f(n-2)) \\ |
| 96 | +&= f(n-1) |
| 97 | +\end{aligned} |
| 98 | +$$ |
| 99 | + |
| 100 | +After simplification, we get the simplified recursive formula: |
| 101 | + |
| 102 | +$$ |
| 103 | +\begin{aligned} |
| 104 | +n \times f(n) &= n \times f(n-1) \\ |
| 105 | +f(n) &= f(n-1) |
| 106 | +\end{aligned} |
| 107 | +$$ |
| 108 | + |
| 109 | +Since we know that $f(1)=1$ and $f(2)=0.5$, therefore when $n \ge 3$, according to $f(n) = f(n-1)$, we know that for any positive integer $n$, $f(n)=0.5$. And since $f(2)=0.5$, therefore when $n \ge 2$, for any positive integer $n$, $f(n)=0.5$. |
| 110 | + |
| 111 | +From this, we can get the result of $f(n)$: |
| 112 | + |
| 113 | +$$ |
| 114 | +f(n) = \begin{cases} |
| 115 | +1.0, & n = 1 \\ |
| 116 | +0.5, & n \ge 2 |
| 117 | +\end{cases} |
| 118 | +$$ |
| 119 | + |
41 | 120 | <!-- tabs:start -->
|
42 | 121 |
|
43 | 122 | ### **Python3**
|
|
0 commit comments