Airplane Seat Assignment Probability — LeetCode 1227 Python Solution

MediumBrainteaserMathDynamic ProgrammingProbability and Statistics
Problem
#1227
Reading time
2 min

The problem

n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly.

Example

Input
n = 1
Output
1.00000
Explanation
The first person can only get the first seat.

Python solution

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

Complexity

MeasureComplexity
TimeO(n^2), which cannot pass all test cases, so it needs to be optimized
SpaceO(1) auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 1227. Airplane Seat Assignment Probability is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1227. Airplane Seat Assignment Probability?
LeetCode 1227. Airplane Seat Assignment Probability is rated Medium on LeetCode.
What is the time complexity of LeetCode 1227. Airplane Seat Assignment Probability?
The Python solution on this page runs in O(n^2), which cannot pass all test cases, so it needs to be optimized.
What is the space complexity of LeetCode 1227. Airplane Seat Assignment Probability?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1227. Airplane Seat Assignment Probability cover?
LeetCode 1227. Airplane Seat Assignment Probability is tagged Brainteaser, Math, Dynamic Programming and Probability and Statistics on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview