Skip to content

Commit

Permalink
Create 1227.AirplaneSeatAssignmentProbability.py
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSpiridonov authored Dec 26, 2020
1 parent f7596ff commit 22ec0be
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Medium/1227.AirplaneSeatAssignmentProbability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'''
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 passengers will:
- Take their own seat if it is still available,
- Pick other seats randomly when they find their
seat occupied
What is the probability that the n-th person can get his
own seat?
Example:
Input: n = 1
Output: 1.00000
Explanation: The first person can only get the first seat.
Example:
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 <= 10^5
'''
#Difficulty: Medium
#100 / 100 test cases passed.
#Runtime: 32 ms
#Memory Usage: 14.2 MB

#Runtime: 32 ms, faster than 49.09% of Python3 online submissions for Airplane Seat Assignment Probability.
#Memory Usage: 14.2 MB, less than 23.33% of Python3 online submissions for Airplane Seat Assignment Probability.

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

0 comments on commit 22ec0be

Please sign in to comment.