#1227

Airplane Seat Assignment Probability

Medium
MathDynamic ProgrammingBrainteaserProbability and StatisticsProbability and StatisticsDynamic Programming (in terms of recognizing patterns)
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution leverages the observation that the probability of the nth passenger sitting in their own seat is always 1/2 for n >= 2. This is because the first passenger's choice creates a 50/50 chance for the last passenger, regardless of the number of passengers.

⚙️

Algorithm

3 steps
  1. 1Step 1: If n = 1, return 1.0 (the only passenger sits in their seat).
  2. 2Step 2: If n = 2, return 0.5 (the first passenger has an equal chance of taking either seat).
  3. 3Step 3: For n >= 3, return 0.5 (the nth passenger's chance remains constant).
solution.py4 lines
1def optimal_probability(n):
2    return 1.0 if n == 1 else 0.5
3
4print(optimal_probability(2))

Complexity note: The optimal solution runs in constant time O(1) and uses constant space O(1) because it only checks the value of n and returns a fixed result.

  • 1The probability for the nth passenger to sit in their own seat stabilizes at 0.5 for n >= 2.
  • 2The first passenger's random choice creates a chain of dependencies that ultimately leads to a 50/50 chance for the last passenger.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.