LeetCode 1227. Airplane Seat Assignment Probability解题报告(python)

本文探讨了一个有趣的问题:在一架有n个座位的飞机上,如果有n名乘客登机,但第一位乘客遗失了机票并随机选择座位,之后的乘客将如何选择座位?文章详细解析了在不同情况下乘客座位选择的概率,并给出了Python实现的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1227. Airplane Seat Assignment Probability

  1. Airplane Seat Assignment Probability python solution

题目描述

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?
在这里插入图片描述

解析

我们考虑n>1的情况
当第一个人到来坐在了位置k上,那么第(2到k-1)的人会坐在他们本身的位置上。所以第k个人就有3种选择,
(1)选择坐在第一个位置上,那么第n个人肯定会坐在位置n上
(2)选择坐在第n个位置上,那么第n个人无法坐在属于自己的位置上
(3)选择坐在k+1到n的位置中间
如果他选择了(3)那么会陷入循环。假设他选择了位置j,则第j个人会面临同样的选择。而选择(1)和选择(2)是等概率,所以最终的概率应该是0.5

class Solution:
    def nthPersonGetsNthSeat(self, n: int) -> float:
        if(n==1):
            return(1.0)
        else:
            return(0.5)

Reference

https://leetcode.com/problems/airplane-seat-assignment-probability/discuss/407707/O(1)-space-O(1)-runtime-with-intuitive-explanation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值