Algorithm


Problem Name: 1227. Airplane Seat Assignment Probability

 

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 first person gets the first seat).

 

Constraints:

  • 1 <= n <= 105

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


class Solution:
    def nthPersonGetsNthSeat(self, n: int) -> float:
        return max(0.5, 1 / n)
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 1

Output

x
+
cmd
1.00000

#2 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _1227_AirplaneSeatAssignmentProbability
    {
        public double NthPersonGetsNthSeat(int n)
        {
            return n == 1 ? 1.0 : 0.5;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 1

Output

x
+
cmd
1.00000
Advertisements

Demonstration


Previous
#1224 Leetcode Maximum Equal Frequency Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1232 Leetcode Check If It Is a Straight Line Solution in C, C++, Java, JavaScript, Python, C# Leetcode