题目:
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 classExamRoom(int N)
that exposes two functions:
ExamRoom.seat()
returning an int representing what seat the student sat in, andExamRoom.leave(int p)
representing that the student in seat numberp
now leaves the room. It is guaranteed that any calls toExamRoom.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 toExamRoom.leave(p)
are guaranteed to have a student currently sitting in seat numberp
.
解释:
与849. Maximize Distance to Closest Person(python+cpp) ,821. Shortest Distance to a Character(python+cpp)类似,但是是实现一个类,而且不是返回最大距离而是返回应该坐下的位置的index如果有多个位置可以坐下,返回最小的index。
用一个数组seats
来记录有人做的位置的index,对于数组seats
错位求距离,如果距离大于当前的最大距离,则这两个位置中间的位置就是一个可以坐的位置,插入数组中,注意,数组要随时保持排序。
python代码:
import bisect
class ExamRoom(object):
def __init__(self, N):
"""
:type N: int
"""
#seats用于存储有人坐的位置的index
self.seats=[]
self.N=N
def seat(self):
"""
:rtype: int
"""
if not self.seats:
res=0
else:
d,res=self.seats[0],0
for a, b in zip(self.seats,self.seats[1:]):
if (b-a)/2>d:
d=(b-a)/2
res=(a+b)/2
if self.N-1-self.seats[-1]>d:
res=self.N-1
bisect.insort(self.seats,res)
return res
def leave(self, p):
"""
:type p: int
:rtype: void
"""
self.seats.remove(p)
# Your ExamRoom object will be instantiated and called as such:
# obj = ExamRoom(N)
# param_1 = obj.seat()
# obj.leave(p)
c++代码:
class ExamRoom {
public:
int global_N;
vector<int>seats;
ExamRoom(int N) {
global_N=N;
}
int seat() {
int res;
if (seats.size()==0)
res=0;
else
{
int d=seats[0];
res=0;
for (int i=0;i<seats.size()-1;i++)
{
int a=seats[i],b=seats[i+1];
if((b-a)/2>d)
{
d=(b-a)/2;
res=(a+b)/2;
}
}
if (global_N-1-seats[seats.size()-1]>d)
res=global_N-1;
}
//寻找第一个大于res的数的
auto index=lower_bound(seats.begin(),seats.end(),res);
seats.insert(index,res);
return res;
}
void leave(int p) {
for (vector<int>::iterator iter = seats.begin(); iter != seats.end();)
{
if (*iter==p)
{
iter = seats.erase(iter);
break;
}
else
iter++;
}
}
};
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom obj = new ExamRoom(N);
* int param_1 = obj.seat();
* obj.leave(p);
*/
总结:
注意c++中删除指定元素的写法(python直接用remove()
就可以删除list中指定元素的第一个匹配项)
for (vector<int>::iterator iter = lst.begin(); iter != lst.end();)
{
if (符合条件)
{
iter = lst.erase(iter);
}
else
{
iter++;
}
}
lower_bound()
返回大于指定元素的第一个值的指针,所以直接在这里插入就好,插入的时候也是传入指针。