forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1227.AirplaneSeatAssignmentProbability.py
- Loading branch information
1 parent
f7596ff
commit 22ec0be
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |