#1227
Airplane Seat Assignment Probability
specialist · 670 · lc medium +30 · verified · 67.2% accepted · 658 likes · top 73%
Description
n passengers board a plane that has exactly n seats. The first passenger lost their ticket and randomly picks any seat. Each subsequent passenger:
- Takes their assigned seat if it is still free, or
- Randomly picks one of the remaining empty seats if their own is already taken.
Return the probability that the nth passenger ends up in their own assigned seat.
Example 1:
Input: n = 1
Output: 1.00000
Explanation: The first person can only get the first seat.
Example 2:
Input: n = 2
Output: 0.50000
Explanation: The second person has a probability of 0.5 to get the second seat (when first person gets the first seat).
Code
1
2
3