作者: 负雪明烛
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. 1 <= N <= 10^9
  2. ExamRoom.seat() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
  3. 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)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. shell批量创建用户

    #!/bin/bash cat << EOF ************************************************************ 批量添加用户并随机生 ...

  2. Linux升级命令yum upgrade和yum update的区别

    Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的: yum -y update 升级所有包同时也升级软件和系统内核 yum -y upgrade 只 ...

  3. 安装octave详解

    1. 一些可以替换的库(可跳过) 默认的库安装libblas.dll.OpenBLAS-v2.6.0-0-54e7b37_dynamicarch_nt4(自动检测CPU类型) 在目录下<your ...

  4. jquery chosen onchange 值改变时触发方法

    jquery chosen onchange 值改变时触发方法如下:$(".chzn-select").chosen().on("change", functi ...

  5. MapReduce01 概述

    MapReduce 概述 目录 MapReduce 概述 1.定义 2.优缺点 优点 缺点 3.MapReduce核心思想 4.MapReduce进程 5.官方 WordCount 源码 6.常用数据 ...

  6. aboard, abolish

    aboard board做动词有上车/船/飞机的意思,boarding就是正在上.board做名词有板的意思,车厢地板的板. a是个词根,有三种意思:1. 以某种状态或方式,如: ablaze, af ...

  7. 100个Shell脚本——【脚本4】自定义rm命令

    [脚本4]自定义rm命令 linux系统的rm命令太危险,一不小心就会删除掉系统文件. 写一个shell脚本来替换系统的rm命令,要求当删除一个文件或者目录时,都要做一个备份,然后再删除.下面分两种情 ...

  8. spring boot集成mybatis框架

    概述 中文官网:http://www.mybatis.cn 参考教程:https://www.w3cschool.cn/mybatis MyBatis Plus:http://mp.baomidou. ...

  9. 30个类手写Spring核心原理之AOP代码织入(5)

    本文节选自<Spring 5核心原理> 前面我们已经完成了Spring IoC.DI.MVC三大核心模块的功能,并保证了功能可用.接下来要完成Spring的另一个核心模块-AOP,这也是最 ...

  10. Jenkins配置代码化

    目录 一.简介 二.init.groovy 脚本命令行调试 一.简介 Jenkins用久了,会有一种莫名的紧张感.因为没人清楚Jenkins都配置了什么,以至于没人敢动它. 但凡使用界面进行配置的都会 ...