[LeetCode] Exam Room 考试房间
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^9ExamRoom.seat()andExamRoom.leave()will be called at most10^4times across all test cases.- Calls to
ExamRoom.leave(p)are guaranteed to have a student currently sitting in seat numberp.
这道题是之前那道 [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
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] Exam Room 考试房间的更多相关文章
- [转]RPA流程自动化-Blueprism认证考试介绍
本文转自:https://www.cnblogs.com/digod/p/9190186.html RPA流程自动化-Blueprism认证考试介绍 接触RPA有一段时间了,几种RPA相关工具也都试用 ...
- RPA流程自动化-Blueprism认证考试介绍
RPA流程自动化-Blueprism认证考试介绍 接触RPA有一段时间了,几种RPA相关工具也都试用过,BluePrism是RPA工具的一种,今天跟大家分享考Blueprism的一些经验. RPA(R ...
- 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 ...
- Vue回炉重造之搭建考试答卷系统
本篇章主要讲述系统搭建逻辑,有疑问的可以加微信联系我.考试系统 资源 Vue.js Element UI 第三方数据接口 业务 答题过程中,防止用户中途退出或者其他不可抗力因素阻碍答题,在每次选择都要 ...
- weex 小结 -- <list>
可以包含各种子组件 <refresh style="width:750;padding:30;flex-direction:row;justify-content:center;&qu ...
- java经典题目
/***********Ryear.java begin********************/ import java.util.Scanner;public class Ryear { /** ...
- News common vocabulary
英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- nodeJS实现简单网页爬虫功能
前面的话 本文将使用nodeJS实现一个简单的网页爬虫功能 网页源码 使用http.get()方法获取网页源码,以hao123网站的头条页面为例 http://tuijian.hao123.com/h ...
随机推荐
- 各种手工DIY
http://www.guokr.com/scientific/subject/diy/ 高仿宜家莲花灯 http://www.guokr.com/article/179863/ 做一只会发光的魔幻吊 ...
- Python——字符串2.0(实验)
直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改
- jquery +ajax 上传加预览
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- OpenCV-Python:模板匹配
啥叫模板匹配 模板匹配就是在大图中找小图,也就说在一幅图像中寻找另一幅模板图像的位置: OpenCV使用 cv2.matchTemplate() 实现模板匹配. import cv2 import n ...
- [转] 如何设置双网卡同时连接内网外网_bpao_新浪博客
已剪辑自: http://blog.sina.com.cn/s/blog_5d3e229c0100skwe.html 如何设置双网卡同时连接内网外网 . 通过无线网络连接外网,确保连接成功后开始第二步 ...
- net core EF 链接mysql 数据库
这个主要是一个demo.就在一个工程里面写的 安装MySql.Data.EntityFrameworkCore 增加DbContext 相当于程序与数据库的中间层 public class Ident ...
- git ignore 总结
git ignore 总结 忽略和 ! 不忽略的先后顺序 gitignore的规则是有从上到下的顺序的,所以当我们使用 ! 不忽略的时候,这个顺序会对结果产生影响 例如: # 忽略所有 folder ...
- Linux-day2-pdf课件
1.Linux文件属性和权限 2.Linux重定向 3.Linux文件查找 4.Linux压缩打包 5.课堂作业 权限相关作业: 题目创建用户carol,ivy,jenny,kevin,alice创建 ...
- Navicat Premium 12.1.11.0安装与激活
本文介绍Navicat Premium 12.1.11.0的安装.激活与基本使用. 博主所提供的激活文件理论支持Navicat Premium 12.0.x系列和Navicat Premium 12. ...
- Win10 教育版
Windows 10 版本 1607 引入了专为 K-12 机构的特有需求而设计的两个版本:Windows 10 专业教育版和 Windows 10 教育版. 这些版本为不断发展的 K-12 教育 I ...