forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1227.AirplaneSeatAssignmentProbability.py
37 lines (31 loc) · 1.16 KB
/
1227.AirplaneSeatAssignmentProbability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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