Java实现 LeetCode 1227 飞机座位分配概率
1227. 飞机座位分配概率
有 n 位乘客即将登机,飞机正好有 n 个座位。第一位乘客的票丢了,他随便选了一个座位坐下。
剩下的乘客将会:
如果他们自己的座位还空着,就坐到自己的座位上,
当他们自己的座位被占用时,随机选择其他座位
第 n 位乘客坐在自己的座位上的概率是多少?
示例 1:
输入:n = 1
输出:1.00000
解释:第一个人只会坐在自己的位置上。
示例 2:
输入: n = 2
输出: 0.50000
解释:在第一个人选好座位坐下后,第二个人坐在自己的座位上的概率是 0.5。
提示:
1 <= n <= 10^5
PS:
分析:(头和尾属于特殊乘客我们单独分析)
如果 n = 5,中间的 3 个人至少有 2 个人会坐在自己的座位上
如果 n = 9,中间的 7 个人至少有 6 个人会坐在自己的座位上
因为第一个丢票的人,最多只能占据中间有票的一个座位
所以在 3 以上的情况无论 n = ?,都简化为了 n = 3,求下面三种情况的概率之和
1.如果第一个人坐在自己的座位上
- 第一个人坐在自己的座位上的概率为 1/3,接着第二个人的座位是空的,这时候,第二个人会坐到自己的座位上,所以最后一个乘客坐到自己的座位的概率是 1/1,这种情况的概率是 1/3 * 1/1
2.如果第一个人坐在第二个有票乘客的座位上
- 第一个人坐在第二个乘客的座位上的概率为 1/3,这时候第二个乘客的座位是空的,他会随便坐,他没有坐到最后一个乘客座位的概率是 1/2,这时候,最后一个乘客只有一个自己的座位了,可以坐上,这种情况的概率是 1/3 * 1/2
3.如果第一个人坐在第三个乘客的座位上
- 第一个人坐在第三个有票乘客的座位上的概率为 1/3,这时候可以确定最后一个乘客一定不会坐到自己的座位上了,这种情况的概率是 1/3 * 0
将以上情况的概率相加即为 n = 3 以上情况的答案:
1/3 * 1/1 + 1/3 * 1/2 + 1/3 * 0 = 0.5
class Solution {
public double nthPersonGetsNthSeat(int n) {
return n == 1 ? 1:0.5;
}
}
Java实现 LeetCode 1227 飞机座位分配概率的更多相关文章
- L1-049 天梯赛座位分配 (20 分)
L1-049 天梯赛座位分配 (20 分)(Java解法) 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- 【跟我一起读 linux 源码 01】boot
计算机启动流程在我的上一个学习计划<自制操作系统>系列中,已经从完全不知道,过渡到了现在的了如指掌了,虽然有些夸张,但整个大体流程已经像过电影一样在我脑海里了,所以在看 linux 源码的 ...
- 关于SpringBoot的外部化配置使用记录
关于SpringBoot的外部化配置使用记录 声明: 若有任何纰漏.错误请不吝指出! 记录下使用SpringBoot配置时遇到的一些麻烦,虽然这种麻烦是因为知识匮乏导致的. 记录下避免一段时间后自己又 ...
- 手机网页,div内滚动条,以及div内部滚动条拉到底部之后触发事件
var gao = document.documentElement.clientHeight; var headHeight = parseInt($('.yhead').css('height') ...
- Spring全家桶之springMVC(二)
spring mvc中url-pattern的写法 1.设置url-pattern为*.do 之前我们在web.xml文件中配置DispatcherServlet的时候,将url-pattern配置为 ...
- Linux之cat的使用介绍
cat选项分析 ...
- ESXI 6.5利用Centos7重置root密码
ESXI6.5宿主机,很久没有登录,再次登录的时候,发现忘记root密码了 1.先将刻录一个CentOS7的启动光盘或U盘,并将服务器的启动项修改为光盘 2.保存BIOS重启后,选择Troublesh ...
- kafka消费者重试逻辑的实现
背景 在kafka的消费者中,如果消费某条消息出错,会导致该条消息不会被ack,该消息会被不断的重试,阻塞该分区的其他消息的消费,因此,为了保证消息队列不被阻塞,在出现异常的情况下,我们一般还是会ac ...
- Codeforces1144A(A题)Diverse Strings
A. Diverse Strings A string is called diverse if it contains consecutive (adjacent) letters of the L ...
- Django之ORM外部python脚本使用
python脚本使用django的ROM 如果你想通过自己创建的python文件在django项目中使用django的models,那么就需要调用django的环境: 在总的项目文件夹创建的py文件: ...
- 汉语分词工具包jieba
#分词pip install jieba import jieba str="你真的真不知道我是谁吗?" res1=jieba.cut(str) print(list(res1)) ...