题目如下:
n
passengers board an airplane with exactlyn
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
解题思路:我计算了n=3,4,5时的概率,发现都是0.5,所以就有了一个大胆的猜想,除了n=1之外,其他的概率都是0.5。
代码如下:
class Solution(object): def nthPersonGetsNthSeat(self, n): """ :type n: int :rtype: float """ if n == 1 : return float(1) return 0.50000