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(p) are guaranteed to have a student currently sitting in seat number p.

这道题是之前那道 [Maximize Distance to Closest Person](https://www.cnblogs.com/grandyang/p/10503789.html) 的拓展,说是有个考场,每个考生入座的时候都要尽可能的跟左右两边的人距离保持最大,当最大距离相同的时候,考生坐在座位编号较小的那个位置。对于墙的处理跟之前那道是一样的,能靠墙就尽量靠墙,这样肯定离别人最远。其实在之前那道题 [Maximize Distance to Closest Person](https://www.cnblogs.com/grandyang/p/10503789.html) 最后的讨论部分博主就预言了这道题,当时博主还没有看这道题,果然是要我们返回座位的具体位置。

博主最先想的方法是用一个大小为N的数组来表示所有的座位,初始化为0,表示没有一个人,若有人入座了,则将该位置变为1,离开则变为0,那么对于 leave() 函数就十分简单了,直接将对应位置改为0即可。重点就是 seat() 函数了,这个可以借鉴之前那道 Maximize Distance to Closest Person 的思路,采用双指针来做,主要就是找连续的0进行处理,还是要分 start 是否为0的情况,因为空位从墙的位置开始,跟空位在两人之间的处理情况是不同的,若空位从墙开始,肯定是坐墙边,而若是在两人之间,则需要坐在最中间,还要记得更新 start 为下一个空座位。最后在处理末尾空位连到墙的时候,跟之前稍有些不同,因为题目要求当最大距离相同的时候,需要选择座位号小的位置,而当此时 start 为0的时候,说明所有的位置都是空的,那么我们不需要再更新 idx 了,就用其初始值0,表示就坐在第一个位置,是符合题意的。最后别忘了将 idx 位置赋值为1,表示有人坐了。

看到这里,你一定以为大功告成了吧,but,OJ总是不断给人惊喜,Time Limit Exceeded,貌似不让我们这么轻易过关啊。那么只能进行优化了,首先分析上面的思路哪块比较耗时,其实就是遍历,当有大量的空座位的时候,中间的0还得一个个遍历,不是很高效,那么比较直接的改进方法就是去掉那些0,我们只保存有人坐的位置,即所有1的位置。这样省去了遍历0的时间,大大提高了效率,此时我们就可以使用 TreeSet 来保存1的位置,其余部分并不怎么需要改变,在确定了座位 idx 时,将其加入 TreeSet 中。在 leave() 中,直接移除离开人的座位位置即可,参见代码如下:

class ExamRoom {
public:
ExamRoom(int N) {
n = N;
} int seat() {
int start = 0, mx = 0, idx = 0;
for (int i : spots) {
if (start == 0) {
if (mx < i - start) {
mx = i - start;
idx = 0;
}
} else {
if (mx < (i - start + 1) / 2) {
mx = (i - start + 1) / 2;
idx = start + mx - 1;
}
}
start = i + 1;
}
if (start > 0 && mx < n - start) {
mx = n - start;
idx = n - 1;
}
spots.insert(idx);
return idx;
} void leave(int p) {
spots.erase(p);
} private:
int n;
set<int> spots;
};

讨论:若这道题还有 follow up 的话,那么博主能想到的就是变成二维的数组,这样才更像一个真正的考场啊,毕竟大多数考场都不是只有一排座位的。变成了二维的话,那么周围四面八方的人的距离都要考虑呢,想想觉得还挺难的,大家有什么想法的话,欢迎留言讨论哈~


Github 同步地址:

https://github.com/grandyang/leetcode/issues/855

类似题目:

Maximize Distance to Closest Person

参考资料:

https://leetcode.com/problems/exam-room/

https://leetcode.com/problems/exam-room/discuss/139862/C%2B%2BJavaPython-Straight-Forward

https://leetcode.com/problems/exam-room/discuss/148595/Java-PriorityQueue-with-customized-object.-seat%3A-O(logn)-leave-O(n)-with-explanation

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] Exam Room 考试房间的更多相关文章

  1. [转]RPA流程自动化-Blueprism认证考试介绍

    本文转自:https://www.cnblogs.com/digod/p/9190186.html RPA流程自动化-Blueprism认证考试介绍 接触RPA有一段时间了,几种RPA相关工具也都试用 ...

  2. RPA流程自动化-Blueprism认证考试介绍

    RPA流程自动化-Blueprism认证考试介绍 接触RPA有一段时间了,几种RPA相关工具也都试用过,BluePrism是RPA工具的一种,今天跟大家分享考Blueprism的一些经验. RPA(R ...

  3. TPO-15 C2 Performance on a biology exam

    TPO-15 C2 Performance on a biology exam 第 1 段 1.Listen to part of a conversation between a Student a ...

  4. Vue回炉重造之搭建考试答卷系统

    本篇章主要讲述系统搭建逻辑,有疑问的可以加微信联系我.考试系统 资源 Vue.js Element UI 第三方数据接口 业务 答题过程中,防止用户中途退出或者其他不可抗力因素阻碍答题,在每次选择都要 ...

  5. weex 小结 -- <list>

    可以包含各种子组件 <refresh style="width:750;padding:30;flex-direction:row;justify-content:center;&qu ...

  6. java经典题目

    /***********Ryear.java begin********************/ import java.util.Scanner;public class Ryear { /** ...

  7. News common vocabulary

    英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 ...

  8. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

  9. nodeJS实现简单网页爬虫功能

    前面的话 本文将使用nodeJS实现一个简单的网页爬虫功能 网页源码 使用http.get()方法获取网页源码,以hao123网站的头条页面为例 http://tuijian.hao123.com/h ...

随机推荐

  1. Loading class `com.mysql.jdbc.Driver'. The new driver class is `com.mysql.cj.jdb 问题

    是因为最新的数据库驱动的原因,用较早的版本就可以了. <dependency> <groupId>mysql</groupId> <artifactId> ...

  2. 20175333曹雅坤MyCP(课下作业,必做)

    MyCP(课下作业,必做) 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin ...

  3. linux常用技能

    阿里云镜像图形界面克隆虚拟机 linux替换阿里云镜像 centos6.6安装图形界面 克隆虚拟机后网络问题 linux替换阿里云镜像 第一步:备份你的原镜像文件,以免出错后可以恢复. cp /etc ...

  4. Win10如何彻底禁用小娜?彻底禁用小娜的方法

    原文地址:https://www.cnblogs.com/zhuimengle/p/5949152.html 小娜是Win10系统中的一款强大功能,有了它,我们对电脑的操作将更加方便.Win10系统有 ...

  5. Windows XP 的最高版本 .net framework 安装

    注意: Windows XP 系统已于2014年4月8日停止维护.出于安全.性能及新设备支持考虑,应立即停止使用. 安装 Windows XP SP3 所支持的最高.net framework 版本为 ...

  6. 关于COOKIE在本地可以正常写入发布后不能写入浏览器的问题

    看了一下cookie的属性设置如下: HTTP Cookie       设置了secure ,   该cookie只能在HTTPS通道下被写入浏览器. HTTPS Cookie     设置了sec ...

  7. 写一个python 爬虫爬取百度电影并存入mysql中

    目标是利用python爬取百度搜索的电影 在类型 地区 年代各个标签下 电影的名字 评分 和图片连接 以及 电影连接 首先我们先在mysql中建表 create table liubo4( id in ...

  8. unity iOS本地代码总结(一)

    1. 项目能直接运行了,但是代码的实际数据流动任然会有问题. 2. unity的代码能这么简单的被调用简直是奇迹一样,不需要大的改动就能够使用. 3. 目前需要注意的问题就是,unity的内容还太少, ...

  9. web.xml 各版本的 Schema 头部声明

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  10. html中div使用CSS实现水平/垂直居中的多种方式

    CSS中的居中,在工作中,会经常遇到.它可以分为水平居中和垂直居中,以下是几种实现居中的方式. git 查看源码 配合在线预览,效果更佳 以下例子中,涉及到的CSS属性值. .parent-frame ...