855. Exam Room(python+cpp)

题目:

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()and ExamRoom.leave()will be called at most 10^4 times across all test cases.
Calls to ExamRoom.leave(p) are guaranteed to have a student currently sitting in seat number p.

解释:
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()返回大于指定元素的第一个值的指针,所以直接在这里插入就好,插入的时候也是传入指针。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值