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 ...
随机推荐
- struts2 进阶--异常捕获机制
在SpringMvc中有自己的异常处理机制,struts2当然会有此功能,主要是在struts.xml中配置: <bean type="com.opensymphony.xwork2. ...
- 如何理解golang中的nil
nil的奇怪行为 刚接触golang时,发现nil在不同的上下文,行为表现是不同的,并且和其他语言中的表现,也不大相同 实例1:输入true, true, false,不符合传递性 func main ...
- 搞懂:MVVM模型以及VUE中的数据绑定数据劫持发布订阅模式
搞懂:MVVM模式和Vue中的MVVM模式 MVVM MVVM : model - view - viewmodel的缩写,说都能直接说出来 model:模型,view:视图,view-Model:视 ...
- Django之ORM属性类型和约束条件
ORM属性类型: 1. CharField 字符串字段, 用于较短的字符串. CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该 ...
- Appium自动化(15) - 针对 webview 进行自动化测试
如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html webview 简介 WebVie ...
- FTP上传 -首先上传文件到的那台电脑得安装ftp
/*↓↓↓↓ add upload ftp file 2014-03-16*/ /*↓↓↓↓ add 2014-03-16 ftp upload file*/ var $ftpse ...
- Hyperledger Fabric——balance transfer(五)执行交易
链码安装和实例化之后就可以调用chaincode执行交易,下面分析简单的账户转账操作是如何完成的. 源码分析 1.首先看app.js的路由函数 app.post('/channels/:channel ...
- hdu6314 容斥+数学
题意 : n*m的矩阵 可以涂黑白两色 问至少A行B列为黑色的涂色方案种类数,答案对998244353取模,1<=n,m,A,B<=3000 题解: ans=sum{A,..n}sum( ...
- [推荐]大量 Blazor 学习资源(一)
前言 / Introduction Blazor 是什么? Blazor 允许您使用 C# 而不是 JavaScript 构建交互式 Web UI. Blazor 应用由使用 C#.HTML 和 CS ...
- $工具, 属性, TAB点击切换
$工具方法 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...