Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 2.44 KB

File metadata and controls

61 lines (47 loc) · 2.44 KB

< Previous                  Next >

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 1:

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

Example 2:

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

Related Topics

[Brainteaser] [Math] [Dynamic Programming] [Probability and Statistics]

Hints

Hint 1 Use dynamic programming, dp[i] indicates the probability that the i-th person can get his seat when there're i persons in total. It's okay to start with O(n^2) solution and then optimize it.
Hint 2 Try to find the regular pattern of the result.