Airplane Seat Assignment Probability — LeetCode 1227 Python Solution
MediumBrainteaserMathDynamic ProgrammingProbability and Statistics
- Problem
- #1227
- Pattern
- Dynamic Programming
- Reading time
- 2 min
- Source
- leetcode.com
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.5Complexity
| Measure | Complexity |
|---|---|
| Time | O(n^2), which cannot pass all test cases, so it needs to be optimized |
| Space | O(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.