【LeetCode】855. Exam Room 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/exam-room/description/
题目描述:
In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1.
When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. (Also, if no one is in the room, then the student sits at seat number 0.)
Return a class ExamRoom(int N) that exposes two functions: ExamRoom.seat() returning an int representing what seat the student sat in, and ExamRoom.leave(int p) representing that the student in seat number p now leaves the room. It is guaranteed that any calls to ExamRoom.leave(p) have a student sitting in seat p.
Example 1:
Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]]
Output: [null,0,9,4,2,null,5]
Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the student sits at the last seat number 5.
Note:
- 1 <= N <= 10^9
- ExamRoom.seat() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
- Calls to ExamRoom.leave§ are guaranteed to have a student currently sitting in seat number p.
题目大意
有一个考场里面有N个座位排成一条线,现在每次有个学生进来需要给他安排座位,要求是他的座位和左右两个人的间隔最远。如果有多个满足要求的座位,需要安排在满足要求且序号最小的位置上。第一个进来的人会坐在第一个位置上。
解题方法
看了寒神的做法,直接对这个过程进行模拟。使用一个数组保存现在已经做了的位置的坐标。如果数组是空,那么就坐在0位置上,否则的话需要遍历查找离两边最端的位置在哪。毫无疑问,如果坐在两个位置之间的话,一定需要是坐在正中间才行。但是还需要注意最后一个位置模拟,因为右边没有人做了,坐在最右端的话,和最后一个人的距离是直接相减。找出了位置然后用二分查找进行插入。
这个走人的办法是直接查找出p的位置,然后移走就好。
时间复杂度是O(N),空间复杂度是O(N)。
class ExamRoom(object):
def __init__(self, N):
"""
:type N: int
"""
self.N, self.L = N, list()
def seat(self):
"""
:rtype: int
"""
N, L = self.N, self.L
if not self.L: res = 0
else:
d, res = L[0], 0
# d means cur distance, res means cur pos
for a, b in zip(L, L[1:]):
if (b - a) / 2 > d:
d = (b - a) / 2
res = (b + a) / 2
if N - 1 - L[-1] > d:
res = N - 1
bisect.insort(L, res)
return res
def leave(self, p):
"""
:type p: int
:rtype: void
"""
self.L.remove(p)
# Your ExamRoom object will be instantiated and called as such:
# obj = ExamRoom(N)
# param_1 = obj.seat()
# obj.leave(p)
参考资料:
https://leetcode.com/problems/exam-room/discuss/139862/C++JavaPython-Straight-Forward
日期
2018 年 10 月 18 日 —— 做梦都在科研
【LeetCode】855. Exam Room 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- linux下面升级 Python版本并修改yum属性信息
最近需要在linux下使用python,故需要升级一下python版本,上网查询了一下相关资料,更新了一下linux下面的python环境,记录如下: linux下面升级 Python版本并修改yum ...
- 进程和线程操作系统转载的Mark一下
https://www.cnblogs.com/leisure_chn/p/10393707.html Linux的进程线程及调度 本文为宋宝华<Linux的进程.线程以及调度>学习笔记. ...
- Centos7服务器上RabbitMQ单机安装
一.背景 最近项目中用到了RabbitMQ,但是发现自己本地没有安装,此文记录一下本地RabbitMQ的安装过程.注意不同的系统安装方式略有不同,此处我们记录的是Centos7的安装方式. 二.安装方 ...
- OpenStack——云平台部署
一.配置网络 准备:安装两台最小化的CentOS7.2的虚拟机,分别添加两张网卡,分别为仅主机模式和NAT模式,并且计算节点设置为4G运行内存,50G硬盘 1.控制节点--配置网络 控制节点第一个网卡 ...
- Angular中怎样创建service服务来实现组件之间调用公共方法
Angular组件之间不能互相调用方法,但是可以通过创建服务来实现公共方法的调用. 实现 创建服务命令 ng g service 服务路径/服务名 比如这里在app/services目录下创建stor ...
- FileReader (三) - 网页拖拽并预显示图片简单实现
以下是一个很贱很简单的一个 在网页上图拽图片并预显示的demo. 我是从https://developer.mozilla.org/en-US/docs/Web/API/FileReader#Stat ...
- ListView的item不能点击(焦点冲突问题)
一般这种问题就是item里面有checkbox或button之类抢占焦点的控件,解决方案有2种: 第一种:就是在checkbox或button添加android:focusable="fal ...
- Hibernate 错误的问题
配了好几次的Hibernate,老是在create BeanFactory的时候fail.我是用MyEclipse自带的HIbernate,直接加进去的. private static final T ...
- docker安装jumpserver
注意MySQL的密码设置要有复杂度,否则jumpserver用不了 #先准备一台服务器安装MySQL和redis(注意官网版本要求) root@ubuntu:~# docker pull mysql: ...
- 【Spring Framework】Spring IOC详解及Bean生命周期详细过程
Spring IOC 首先,在此之前,我们就必须先知道什么是ioc,ioc叫做控制反转,也可以称为依赖注入(DI),实际上依赖注入是ioc的另一种说法, 1.谁控制谁?: 在以前,对象的创建和销毁都是 ...