zoukankan      html  css  js  c++  java
  • 【leetcode】1227. 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
  • 相关阅读:
    第一次作业
    第0次作业—姚舜禹17-1
    第三周作业
    第二周作业
    第一周作业
    第零周作业
    第三周作业
    第二周作业
    第一周作业
    第0次作业
  • 原文地址:https://www.cnblogs.com/seyjs/p/11698376.html
Copyright © 2011-2022 走看看