The Airplane Seat Assignment Probability problem is a classic probability puzzle. There are n
passengers boarding an airplane with exactly n
seats. Each passenger has a ticket for a specific seat, and they board the plane in order. The twist is that the first passenger loses their ticket and randomly picks any seat to sit in. Every subsequent passenger:
n
, return the probability that the nth passenger (the last one to board) ends up sitting in their own assigned seat.
Constraints:
n
≤ 105
At first glance, this problem seems to require simulating all possible seat assignments and counting the cases where the last passenger gets their own seat. However, this approach is not efficient for large n
.
Let's think about the process:
n
in a straightforward way.
Let's formalize the reasoning:
P(n)
be the probability that the nth passenger sits in their own seat.n
choices:
1/n
), everyone gets their assigned seat, so the last passenger does too.1/n
), the last passenger cannot sit in their own seat.(n-2)/n
), the problem reduces to the same scenario with n-1
seats and passengers.P(n) = 1/n * 1 + 1/n * 0 + (n-2)/n * P(n-1)
n = 1
, obviously P(1) = 1
.n = 2
, P(2) = 1/2 * 1 + 1/2 * 0 = 0.5
.n ≥ 2
, P(n) = 0.5
.
Conclusion: For n = 1
, the answer is 1.0
. For n ≥ 2
, the answer is 0.5
.
Thus, we can solve this in O(1)
time with a simple conditional check.
Let's walk through the case where n = 3
:
Total probability passenger 3 gets their own seat:
1/3 (Case 1) + 1/6 (Case 2 subcase) = 0.5
Brute-force approach: Simulating all permutations would take O(n!) time and space, which is infeasible for large n
.
Optimized approach: Using the recursive insight and closed-form solution, we simply check if n == 1
or not, so:
The Airplane Seat Assignment Probability problem looks complex at first, but by breaking it down and recognizing its recursive, self-similar structure, we discover that the probability the last passenger gets their own seat is always 0.5 for n ≥ 2
. This allows for an elegant O(1) solution with just a simple check. This is a great example of how understanding the underlying process can lead to a dramatic simplification of what appears to be a complicated problem.
class Solution:
def nthPersonGetsNthSeat(self, n: int) -> float:
return 1.0 if n == 1 else 0.5
class Solution {
public:
double nthPersonGetsNthSeat(int n) {
return n == 1 ? 1.0 : 0.5;
}
};
class Solution {
public double nthPersonGetsNthSeat(int n) {
return n == 1 ? 1.0 : 0.5;
}
}
var nthPersonGetsNthSeat = function(n) {
return n === 1 ? 1.0 : 0.5;
};