[leetcode] 855. Exam Room

441 篇文章 0 订阅
284 篇文章 0 订阅

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. 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§ 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 考场就座

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值