Exam Room leetcode

KiCad 7.0 + 嘉立创打样:从原理图到实物,打造你的双LLCC68 LoRa串口透传板 本文详细介绍了使用KiCad 7.0设计双LLCC68 LoRa串口透传板的完整流程,从原理图设计到嘉立创打样。重点解析了AVR+LLCC68组合的优势、双模块布局的电磁兼容设计、SPI接口配置及射频走线技巧,并提供了实测数据和常见问题解决方案,帮助开发者高效实现物联网设备的低功耗远距离通信。 阅读详情

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. 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(p) are guaranteed to have a student currently sitting in seat number p.

题目大意:一排作为,编号为0到N-1,每次调用seat指令,从当前没有人的座位中挑选一个没有人的座位,使得这个人离其他人的距离最远,如果有多个这样的位置,选择座号最小的。

分析:已经做好的座位,把剩余的空座位分成多个区间,我们选择了一个区间,在区间中增加一个座位,距离最大。

一开始座位都是空,我们模拟两个不能坐的座位,编号-1,N,也就是说一开始只有一个区间。

座位的边界也就是0和N-1需要特殊处理一下。

使用一个优先队列存储所有的区间信息,当seat命令时,从优先队列取出最大的值,更新区间信息。

当leave命令调用是,相当于增加了一个大的区间,之前的两个小区间删除。

需要解决的问题:

1. 如何根据当前离开的座位,快速找到他相邻的两个座位;

2. 优先队列不支持删除任一元素的操作。

这里做了一个trick,使用stl的map,因为他是有序存储的,可以快速定位相邻的座位;另外优先队列存储的是区间的指针值,当需要删除某一个区间的时候,相当于把区间置为不可用即可;另外需要一个辅助的map,快速根据区间端点找到区间信息指针。

seat操作:优先队列弹出以及增加区间的复杂度O(logn),添加元素到set和map的复杂度也是O(logn),所以整个seat操作的时间复杂度为O(logn)

leave操作:set查找和删除logn,map插入也是logn,整个操作的算法复杂度也是logn

代码如下:

struct node {
    int st;
    int ed;
    int valid;
    int length;
    node(int x = 0, int y = 0, int l = 0){
        valid = 1;
        st = x;
        ed = y;
        length = l;
    }
};


struct cmp{
    bool operator()(node *a, node *b){
        if(a->length==b->length) return a->st>b->st;
        return a->length<b->length;
    }
};




class ExamRoom {
public:
    ExamRoom(int N) {
        N_ = N;
        node *p = new node(-1, N, N);
        qe.push(p);
        seats.insert(-1);
        seats.insert(N);
        address[make_pair(-1,N)] = p;
    }
    
    ~ExamRoom(){
        for(auto it = address.begin(); it!=address.end();it++)
        {
            delete (it->second);
        }
    }
    
    void add_node(int st, int ed){
        auto it = address.find(make_pair(st, ed));
        if(it!=address.end()){
            it->second->valid = 1;
        } else {
            int length = (ed-st)/2;
            if(st==-1 || ed==N_)length = ed-st-1;
            node *p = new node(st, ed, length);
            qe.push(p);
            address[make_pair(st, ed)] = p;
        }
    }
    
    int seat() {
        if(qe.size()==0) return 0;
        while(!qe.empty()){
            auto p = qe.top();
            qe.pop();
            if(p->valid==0) {
                auto it = address.find(make_pair(p->st, p->ed));
                if(it!=address.end()){
                    address.erase(it);
                    delete p;
                }
                continue;
            }
            int ret;
            if(p->st==-1)
                ret = 0;
            else if(p->ed==N_)
                ret = N_-1;
            else{
                ret = (p->st+p->ed)/2;
            }
            if(ret-p->st>1){
                add_node(p->st, ret);
            }
            
            if(p->ed-ret>1){
                add_node(ret, p->ed);
            }
            auto it = address.find(make_pair(p->st, p->ed));
            if(it!=address.end()){
                address.erase(it);
                delete p;
            }
            seats.insert(ret);
            return ret;
        }
        return 0;
    }
    
    void leave(int p) {
        auto it = seats.find(p);
        if(it==seats.end()) return;
        seats.erase(it);
        it = seats.upper_bound(p);
        auto high = *it;
        it--;
        auto low = *it;
        auto tmp = address.find(make_pair(low, p));
        if(tmp!=address.end()){
            tmp->second->valid = 0;
        }
        tmp = address.find(make_pair(p, high));
        if(tmp!=address.end()){
            tmp->second->valid = 0;
        }
        add_node(low, high);     
    }
    set<int> seats;
    priority_queue<node*, vector<node*>, cmp> qe;
    map<pair<int, int>, node*> address;
    int N_;
};


/**
 * Your ExamRoom object will be instantiated and called as such:
 * ExamRoom obj = new ExamRoom(N);
 * int param_1 = obj.seat();
 * obj.leave(p);
 */


vue2/3 - element表格组件el-table实现懒加载树型(上下级)数据、默认展开和隐藏层级,支持对表格进行修改|新增|删除,解决完成操作后树节点不刷新问题(提供示例代码,一键复制开箱即用 vue2,vue3,elementui,elementPlus,el-table,Table表格组件,表格懒加载数据,element table表格数中的树形结构懒加载,增加后数据不同步更新问题,el-table树形与el-tree树形结构的一键折叠与展开,elementui中el-tree懒加载数据 默认展开最外层节点,Element-plus Table组件树表结构默认展开一级目录,vue实现element table表格树形结构-使用懒加载时-解决子节点增删改后,不刷新子节点数据问题,解决el-tre 阅读详情

相关推荐

PXE无人值守安装麒麟系统后,如何用.kylin-post-actions文件实现深度定制?

本文详细介绍了在PXE无人值守安装麒麟Kylin V10系统后,如何利用.kylin-post-actions文件实现深度定制。从脚本编写基础到高级定制技巧,包括系统配置自动化、软件包管理策略、用户环境标准化和安全加固自动化,帮助用户快速部署符合生产环境标准的系统配置。

weixin_29306317的博客 235

Leetcode 855. Exam Room 考场就座:提供两种解法

Leetcode 855. Exam Room 考场就座: 提供两种解法855. Exam Room 考场就座(两种解法)题目描述示例:解答1代码1解答2代码2 855. Exam Room 考场就座(两种解法) 题目描述 In an exam room, there are N seats in a single row, numbered 0, 1, 2, …, N-1. When a stu...

importsys的博客 858

人工智能——框架表示法

1975年美国明斯基在“A framework for representing knowledge”提出了框架理论。 1. 框架及框架的一般表示形式 框架:一种描述对象属性的数据结构。 框架的一般表示形式 <框架名> 槽名1: 侧面名1:值1,值2…,值p1 侧面名2:值1,值2…,值p2 侧面名2:值1,值2…,值pm1 槽名2: 侧面名...

weixin_33759269的博客 1645

[LeetCode] Exam Room 考试房间

In an exam room, there areNseats in a single row, numbered0, 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 ...

weixin_30929295的博客 210

Zynq7000混合启动方案:QSPI Flash引导与eMMC ext4文件系统部署实战

本文详细介绍了Zynq7000平台的混合启动方案,结合QSPI Flash引导与eMMC ext4文件系统部署,解决嵌入式Linux系统启动可靠性与大容量存储需求。通过硬件连接配置、分区规划、Petalinux工程配置及实战部署步骤,帮助开发者高效实现系统优化,提升启动速度和文件系统性能。

weixin_29190169的博客 451

37.BswM 多核配置与模块初始化顺序——从单核直线到多核分段

多核AUTOSAR中,BswM由主核(MasterCore)统一仲裁,从核(SlaveCore)按EcucPartition配置本地BSW模块,实现“主核决策、从核执行”。启动时主核先初始化并唤醒从核,关机时反向同步。跨核共享数据需通过Spinlock与IOC保护。关键点:主核负责全局初始化顺序(如NvM须先于通信栈),从核仅按分区执行本地序列,且不可修改自动配置顺序。建议将通信栈集中于BSWCore以减少跨核通信。此模型可平滑迁移至多核平台,提升系统一致性与可靠性。

lin135079的博客 14

LeetCode每日一题(855. Exam Room)

There is an exam room with n seats in a single row labeled from 0 to 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

wangjun861205的博客 198

MATLAB 与 STK 互联导出导弹轨迹及速度数据(csv导出)

本文介绍了如何通过 MATLAB 脚本连接 STK 提取导弹三维轨迹数据,并以 CSV 格式导出,包含以下关键内容:自动连接 STK 场景;获取所有导弹对象;提取 J2000 坐标系下的位置和速度;加入任务优先级与状态;导出为标准 CSV 文件;

weixin_52370774的博客 792

【Leetcode】855. Exam Room

对于区间之间的比较,首先是看dist,即看这个区间如果坐人的话,其与端点的最大距离,大者优先;接着看这个区间坐人的位置,位置小者优先。这样每次只需要在平衡树里找优先级最高的区间就行了。而离位操作,我们只需要将这个人前面的人和后面的人找到,将两段区间删除再加上整个区间即可。要求找到一个空位,使得该位子与离其最近的有人的座位的距离尽量大,返回就位的座位下标;如果有平手,则尽量选下标小的;可以用平衡树维护区间,而人坐的位置就是区间的端点。是每次操作已经有座位的人数。,假设第一个人来了,坐在。

数学、算法爱好者的博客 458

儿童启蒙教育APP

pinyinList.add(createPinyin("a", "啊,像阿姨的阿", R.raw.pinyin_a));pinyinList.add(createPinyin("i", "衣,像衣服的衣", R.raw.pinyin_i));pinyinList.add(createPinyin("u", "乌,像乌鸦的乌", R.raw.pinyin_u));pinyinList.add(createPinyin("ü", "鱼,像小鱼的鱼", R.raw.pinyin_v));-- 固定竖屏 -->

寒寒豆豆软件公司的博客 191

【雷达通信】雷达信号处理仿真【含Matlab源码 2509期】

雷达信号处理仿真 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 916

[leetcode] 855. Exam Room

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. ...

Learning from the mistakes 678

复现《水化硅铝酸钙微结构和性能的分子动力学模拟研究》的分子动力学模拟

本文详细复现了杨军博士论文中关于水化硅铝酸钙(C-A-S-H)凝胶的分子动力学模拟方法。研究内容包括:1)基于11Å Tobermorite构建C-A-S-H分子模型,通过缺陷引入和Al掺杂调节Ca/Si比;2)使用ReaxFF力场模拟交联C-A-S-H的结构与力学性能;3)采用ClayFF力场研究纳米孔道中水和离子传输行为。研究提供了完整的LAMMPS输入脚本、Python分析代码和力场参数设置,为水泥基材料纳米尺度模拟提供可复现的技术方案。

huanghm88的专栏 118

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 enters the room, they must sit in the seat that maximizes t...

博客 2918

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode 855. Exam Room

Problem: 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 p...

weixin_34228387的博客 199

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode #855. Exam Room

题目描述: In an exam room, there areNseats in a single row, numbered0, 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...

LawFile的博客 293

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

LeetCode //C - 855. Exam Room

Summary: The problem involves designing an ExamRoom class to manage seat allocation in a room with n seats, where students always choose the seat that maximizes the distance to the nearest occupied seat. The class must support two operations: seat() to ass

Made in Code 2218

leetcode 855. Exam Room 考场就座

邹九的个人博客 308

[LeetCode] 855. Exam Room 考场

In an exam room, there areNseats in a single row, numbered0, 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 ...

weixin_30549657的博客 233

【LeetCode】855. Exam Room 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/exam-room/description/ 题目描述: In an exam room, there are N seats in a single row, numbered 0, 1, 2, ......

负雪明烛 2528

Leetcode 855: Exam Room (排座位好题)

Leetcode 855: 排位置好题

roufoo的博客 1063

[Swift]LeetCode855. 考场就座 | Exam Room

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30323961的博客 172

LeetCode 题目讲解集合

LeetCode 题目集合 题号 难度 正文 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

MrJustin的博客 333

LeetCode 题目难度及通过率(1-900)(转)

转自 https://github.com/grandyang/leetcode 题号 Title Solution Difficulty 900 RLE Iterator 50.50% Medium 899 Orderly Queue 47.70% Hard 898 Bitwise ORs of Subarrays 34.70% Medium 897 Increasing Order Searc...

zwzcdcll的博客 2795

LeetCode 849. Maximize Distance to Closest Person

[Easy] LeetCode 849. Maximize Distance to Closest Person 链接: https://leetcode.com/problems/maximize-distance-to-closest-person/ 题目描述: In a row of seats, 1 represents a person sitting in that seat, an...

MrJustin的博客 274

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论...

从2026-09-21开始专注于AI,以前是后端研发、大数据研发等 1441
上一篇: leetcode Russian Doll Envelopes
下一篇: redis中SDS实现
贱走偏锋
博客等级 码龄15年 24粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址