LeetCode 855. Exam Room
原题链接在这里: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 <= N <= 10^9
ExamRoom.seat()
andExamRoom.leave()
will be called at most10^4
times across all test cases.- Calls to
ExamRoom.leave(p)
are guaranteed to have a student currently sitting in seat numberp
.
题解:
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的更多相关文章
- [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 ...
- 【LeetCode】855. Exam Room 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/exam-roo ...
- 855. Exam Room
维护一个数据结构要满足:一个教室线性排列的座位 0 ... N-1 调用seat 入座一个距离最近学生最远的座位 调用leave x 离座一个位置为x的学生 由于N最多是 10e9 所以选择维护 学生 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- [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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [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 ...
- [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 ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
随机推荐
- 【C语言】学不会的指针
指针 前言: 指针是C语言程序的核心,刚开始学指针,嗯....这样呀,貌似不难呀:之后开始用指针,&p,p,*p,**p,这些指针在用的时候,额.....什么东东?每次都要想半天,特别是遇到双 ...
- JAVA知识点总结篇(二)
数组 一维数组 声明 数据类型[] 数组名: 数据类型 数组名[]: 分配空间 数组名 = new 数据类型 [数组长度]: 可以在声明的同时分配空间,分配空间之后数组中才能放数据,数组元素都是通过下 ...
- Dubbo使用javassist生成动态类
在服务(本地和远程)暴露的时候会调用proxyFactory.getInvoker方法 具体位置: 本地暴露:ServiceConfig#exportLocal line:538 远程暴露: Serv ...
- python 2种创建多线程的方法
多个线程是可以操作同一个全局变量的,因此,可以通过这种方式来判断所有线程的执行进度 # 第一种方法:将要执行的方法作为参数传给Thread的构造方法 import threading import t ...
- 全栈项目|小书架|服务器端-NodeJS+Koa2 实现评论功能
评论功能分析 上图可以看出评论功能主要实现了:评论的发布.评论列表的展示. 在不考虑子评论以及图片评论的场景下,评论功能主要有以下两个接口: 发布评论 获取评论列表(考虑分页) 评论 Model 的建 ...
- K-匿名算法研究
12月的最后几天,研究了下k匿名算法,在这里总结下. 提出背景 Internet 技术.大容量存储技术的迅猛发 展以及数据共享范围的逐步扩大,数据的自动采集 和发布越来越频繁,信息共享较以前来得更为容 ...
- 【面试突击】-Redis常见面试题(二)
1.什么是Redis?简述它的优缺点? Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到 ...
- JavaWeb 之 JSON
一.概述 1.概念 JSON:JavaScript Object Notation JavaScript对象表示法 2.基本格式 var p = {"name":"张三 ...
- restframework中根据请求的类型修改序列化类
只要在视图中重写get_serializer_class方法就可以,用if对请求的类型进行判断 def get_serializer_class(self): if self.action == &q ...
- 爬虫入门urlib,urlib2的基本使用和进阶
python2中的urlib和urlib2 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的 ...