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 飞机座位分配概率的更多相关文章

  1. L1-049 天梯赛座位分配 (20 分)

    L1-049 天梯赛座位分配 (20 分)(Java解法) 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所 ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 为什么PUSH推送要经常背锅?

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 自从做了推送以后,每隔一段时间就发现有各大的公司 ...

  2. python3语法学习第四天--字符串

    字符串:是python中的常用数据类型 Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用 访问字符串的值: 下标和分片截取 字符串的连接:‘+’ 字符串内置函数挺多,选 ...

  3. [java作业]Fan、求直线交点、Triangle2D、选课

    public class Fan { public static void main(String[] args) { Fan fan1 = new Fan(), fan2 = new Fan(); ...

  4. [hdu4497]分解质因数

    题意:求满足gcd(x,y,z)=G,lcm(x,y,z)=L的x,y,z的解的个数. 大致思路:首先如果L % G != 0那么无解,否则令u = L / G,问题变为,gcd(r,s,t)=1,l ...

  5. Python自动生成100以内加减乘除混合运算题

    import random from random import choice ops = ('+','-','×','÷') ans = [] i=0 while i < 100 : op1 ...

  6. Mysql 常用函数(2)- if 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html if 的作用 根据表达式的某个条件或值结果来执行一 ...

  7. ipad4密码忘记锁定了如何破解

    ipad4更新后被要求输入密码,但很长一段时间后忘记了,一直想不起来,也没有忘记密码的选项,以下是简单的破解方法. 注意:没有备份的资料是要被清空的 一.windows10系统,安装iTunes安装 ...

  8. sql查重去除id最小值

    select order_id FROM yzj_store_order t WHERE (t.user_id,t.order_status) IN ( SELECT user_id,order_st ...

  9. 学习笔记:平衡树-splay

    嗯好的今天我们来谈谈cosplay splay是一种操作,是一种调整二叉排序树的操作,但是它并不会时时刻刻保持一个平衡,因为它会根据每一次操作把需要操作的点旋转到根节点上 所谓二叉排序树,就是满足对树 ...

  10. DPDK开发环境搭建(学会了步骤适合各版本)

    一.版本的选择 首先要说明的是,对于生产来说DPDK版本不是越高越好,如何选择合适的版本? 1.要选择长期支持的版本LTS(Long Term Support) 2.根据当前开发的系统环境选择 可以在 ...