leetcode1227. Airplane Seat Assignment Probability

Posted seyjs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode1227. Airplane Seat Assignment Probability相关的知识,希望对你有一定的参考价值。

题目如下:

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

解题思路:我计算了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

 

以上是关于leetcode1227. Airplane Seat Assignment Probability的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式在 iOS 中切换 AirPlane 模式

codeforces838D - Airplane Arrangements

CF838D Airplane Arrangement

#1227 - 访问被拒绝;您需要此操作的 SUPER 权限 - 如何解决?

Cpanel:#1227 - 访问被拒绝;您需要(至少一个)超级权限才能执行此操作

MySQL用户新建表报1227异常