码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】1227. Airplane Seat Assignment Probability

时间:2019-10-18 16:15:48      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:person   cond   data-   solution   oba   span   其他   with   rest   

题目如下:

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

 

【leetcode】1227. Airplane Seat Assignment Probability

标签:person   cond   data-   solution   oba   span   其他   with   rest   

原文地址:https://www.cnblogs.com/seyjs/p/11698376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!