Airplane Seat Assignment Probability Problem


n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of the passengers will:

Take their own seat if it is still available, and
Pick other seats randomly when they find their seat occupied
Return the probability that the nth person gets his own 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 the first person gets the first seat).


Constraints:

1 <= n <= 105


Solution:

Let us suppose n>1
Now let us suppose the first person goes and sits in the k position. Now we surely know that person(2 to k-1) will sit in their correct position(Because when person 2 to k-1 comes then they will find their place empty and will sit on their respective seats). Now k'th person has 3 choices


Sit on seat 1 in which case the last person will surely sit on his seat.
Sit on the last seat in which case the last person will surely not sit on his seat.
Sit on some seat from k+1 to n-1.

Now if he takes option 3 then he will sit on some seat say "j". Now j will again have three options to sit.
This cycle will continue and every person can sit on the first and last seat will equal probability. Hence the probability will be 0.5

class Solution:
    def nthPersonGetsNthSeat(self, n: int) -> float:
        if(n==1):
            return(1.0)
        else:
            return(0.5)

Java Solution:

class Solution {
    public double nthPersonGetsNthSeat(int n) 
    {
        if(n==1)
        {
            return 1.00000;
        }
        else
        {
            return 0.50000;
}
     }
}



*******Thanks For Visiting*********