Leetcode 1227: Airplane Seat Assignment Probability

grid47
grid47
Exploring patterns and algorithms
Jul 7, 2024 3 min read

There are n passengers boarding an airplane with n seats. The first passenger has lost their ticket and picks a random seat. The remaining passengers sit in their assigned seat if available or pick a random seat if their assigned seat is taken. Return the probability that the nth person gets their own seat.
Problem
Approach
Steps
Complexity
Input: The input consists of a single integer `n` (1 <= n <= 10^5), representing the number of passengers and seats.
Example: n = 3
Constraints:
• 1 <= n <= 10^5
Output: The output is a floating-point number representing the probability that the nth person sits in their own seat.
Example: 0.50000
Constraints:
• Return the answer with a precision of 5 decimal places.
Goal: The goal is to compute the probability that the last passenger sits in their own seat.
Steps:
• If there's only one passenger, they will always sit in their own seat (probability = 1).
• For more than one passenger, the probability follows a pattern where it is 0.5 for n >= 2.
Goal: The problem is designed for n ranging from 1 to 100,000, and the solution should efficiently compute the probability even for large n.
Steps:
• 1 <= n <= 10^5
Assumptions:
• The first passenger randomly selects a seat.
• Each subsequent passenger will either sit in their assigned seat or pick a random seat if theirs is taken.
Input: n = 3
Explanation: For three passengers, the first has a 50% chance of sitting in their assigned seat. If they don't, the third passenger has a 50% chance of sitting in theirs. The total probability for the nth person is 0.5.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus