原题链接在这里:https://leetcode.com/problems/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. 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.

题解:

It is clear to find the bigest interval and assign the middle position back.

Use TreeSet ts to maintain intervals sorted by the distance of interval. At the begining, put a dummy interval into ts.

When calculating the distance of interval, divide by 2. Because it only puts value to its closest.

e.g. N = 10. First 3 seats return 0, 9, 4. Now there are 3 seats between [0,4], and 4 seats between [4,9].

But next seat returns 2 in [0,4], but not 6 in [4,9]. Since 2-0 equals to 2, 6-4 still equals to 2.

And since there are dummy interval, when interval[0] == -1, simple return interval[1]. when interval[1] == N, simple return N-interval[0]-1.

Also have 2 maps to maintain mapping between startpoint and interval, endpoint and interval.

For leave(int p), get the left interval with endMap on p. and right interval with startMap on p.

Merge left and right interval and add merged interval back.

Time Complexity: seat, O(logn). leave, O(logn). n is count of intervals in ts.

Space: O(n).

AC Java:

 class ExamRoom {
int N;
TreeSet<int []> ts;
HashMap<Integer, int []> startMap;
HashMap<Integer, int []> endMap; public ExamRoom(int N) {
this.N = N;
ts = new TreeSet<>((a,b) -> {
if(dist(b) == dist(a)){
return a[0] - b[0];
} return dist(b) - dist(a);
}); startMap = new HashMap<>();
endMap = new HashMap<>();
add(new int[]{-1, N});
} public int seat() {
int [] top = ts.pollFirst(); int pos = 0;
if(top[0] == -1){
pos = 0;
}else if(top[1] == N){
pos = N-1;
}else{
pos = top[0] + (top[1]-top[0])/2;
} add(new int[]{top[0], pos});
add(new int[]{pos, top[1]}); return pos;
} public void leave(int p) {
int [] left = endMap.get(p);
int [] right = startMap.get(p);
int [] merged = new int[]{left[0], right[1]}; remove(left);
remove(right);
add(merged);
} private int dist(int [] interval){
if(interval[0] == -1){
return interval[1];
} if(interval[1] == N){
return N-interval[0]-1;
} return (interval[1]-interval[0])/2;
} private void add(int [] interval){
ts.add(interval);
startMap.put(interval[0], interval);
endMap.put(interval[1], interval);
} private void remove(int [] interval){
ts.remove(interval);
startMap.remove(interval[0]);
endMap.remove(interval[1]);
}
} /**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom obj = new ExamRoom(N);
* int param_1 = obj.seat();
* obj.leave(p);
*/

LeetCode 855. Exam Room的更多相关文章

  1. [LeetCode] 855. Exam Room 考场

    In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1. When a student enter ...

  2. 【LeetCode】855. Exam Room 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/exam-roo ...

  3. 855. Exam Room

    维护一个数据结构要满足:一个教室线性排列的座位 0 ... N-1 调用seat 入座一个距离最近学生最远的座位 调用leave x 离座一个位置为x的学生 由于N最多是 10e9 所以选择维护 学生 ...

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. [LeetCode] Exam Room 考试房间

    In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1. When a student enter ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  8. [Swift]LeetCode855. 考场就座 | Exam Room

    In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1. When a student enter ...

  9. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

随机推荐

  1. TCP Socket 套接字 和 粘包问题

    一.Scoket 套接字 Scoket是应用层(应用程序)与TCP/IP协议通信的中间软件抽象层,它是一组接口.也可以理解为总共就三层:应用层,scoket抽象层,复杂的TCP/IP协议 基于TCP协 ...

  2. C++ Primer 第五版示例gcc源码

    官方资源,原封不动的.对应于GCC,因此文件名是以此命名的. 门牌号: https://github.com/ZeroPhong/Learning-Resource/blob/master/GCC_4 ...

  3. AX 2012 Computed column IIF语句使用

    涉及Dynamics ax 行业已经好几年了,准备总结一些有用的处理方法供大家来参考 首先先介绍下Computed column技术,该技术是微软在AX2012版本中提供的新技术,可以让SQL语句在A ...

  4. @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本

    今天遇到了很奇葩的问题,我本机的是以test环境启动的,调用另一个服务接口的时候返回参数却不同,调用接口是没错,怎么会这样,排查了很久,发现在eureka上注册的另一个服务是testone环境,而这个 ...

  5. idea远程部署SpringBoot项目到Docker

    安装docker服务或者系统学习docker参考这篇文档:https://shimo.im/docs/fE0eJCx8IIojQXzB/ 1.配置docker的远程端口 vim /usr/lib/sy ...

  6. 解决h5版的uniapp请求跨域问题

    uni项目里有个名为manifest.json文件 编辑manifest.json文件,找到h5选项,里面一般有个名为“devServer”索引,往这个索引中添加一个“proxy”或者编辑,值为请求域 ...

  7. kie-api介绍和使用

    参考:KIE kie在drools jbpm uberfire里广泛被使用,下面对kie-api中的几个重要组件做下简单介绍 maven依赖 <dependency> <groupI ...

  8. 【解决方案】ArcGIS导入要素集后没反应

    内容源自:ArcGIS10.2基础教程(丁华) 书上要求: 1.在“练习”文件夹中新建一个名为“沈阳”的个人地理数据库和名为“shenyang”的要素集,设置地理坐标为“Xi'an 1980”,高程坐 ...

  9. C# vb .net实现缩放特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的缩放特效呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  10. jquery.pagination.js分页demo

    公用jquery.pagination.js /** * This jQuery plugin displays pagination links inside the selected elemen ...