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§ 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() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
- Calls to ExamRoom.leave§ are guaranteed to have a student currently sitting in seat number p.
分析
题目的意思是:有一间教室,教室的座位成一排,还编了号。现在设定了一些规则,如果教室为空,则要坐在座位0,如果不空,则需要坐在离最近的人的座位尽量远。
- 这里用集合s来判断座位是否被坐,n为容量。然后我们就根据题目给的规则来写代码就行了。
- 如果set中没有元素,那么返回0,并且把0插入到set中。
- 如果set中元素不为空,那么就是常规一个一个区间的比较,但是要注意的是,假如长度为10,可能会有[1,7]出现在set中,那么就需要把左区间[0,1]和右区间[7,9]考虑进去。
代码
class ExamRoom {
private:
set<int> s;
int n;
public:
ExamRoom(int N) {
n=N;
}
int seat() {
int in_idx=0;
if(s.size()==0){
s.insert(0);
return 0;
}
if(s.size()){
int maxLen=0;
if(!s.count(0)){ //0 没有被占用 [0,x1]
maxLen=*s.begin();
in_idx=0;
}
int idx=0;
auto begin=s.begin();
auto end=s.end();
while(begin!=end){ //记录上一个人的位置[x1,x2]....
int len=(*begin-idx)/2;
if(len>maxLen){
maxLen=len;
in_idx=(*begin+idx)/2;
}
idx=*begin;
begin++;
}
//最后的位置没有被占
if(!s.count(n-1)){ //xn~末尾
int len=n-1-*s.rbegin();
if(len>maxLen){
maxLen=len;
in_idx=n-1;
}
}
}
s.insert(in_idx);
return in_idx;
}
void leave(int p) {
s.erase(s.find(p));
}
};
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom obj = new ExamRoom(N);
* int param_1 = obj.seat();
* obj.leave(p);
*/
参考文献
855. Exam Room
Exam Room 考场就座