LeetCode #855. Exam Room

09 SAP CPI 访问公共API接口 我在网上找了一个公共API,入参输入汉字返回拼音。至此,一个真正的访问API的接口就做好了。点击保存时可看到红叉报错。 阅读详情

题目描述:

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.
class ExamRoom {
public:
    ExamRoom(int N) {
        n=N;
    }
    
    int seat() {
        if(s.empty())
        {
            s.insert(0);
            return 0;
        }
        bool first=s.count(0);
        bool last=s.count(n-1);
        int pre=0, result=0, max_dist=0;
        // 遍历set时,需要单独考虑两个端点,当它们没有占用时可以考虑
        // 而且计算两个端点到其他点的距离时不需要除二
        for(const int& i:s)
        {
            int dist=(pre==0&&!first)?i-pre:(i-pre)/2;
            if(dist>max_dist)
            {
                max_dist=dist;
                if(pre==0&&!first) result=0;
                else result=(i+pre)/2;
            }
            pre=i;
        }
        if(n-1-pre>max_dist&&!last) result=n-1;
        s.insert(result);
        return result;
    }
    
    void leave(int p) {
        s.erase(p);
    }

private:
    int n;
    set<int> s;
};

 

国家自然基金研究方向选定后,关键词只能下拉吗?——不是的,国家自然科学基金(NSFC)的关键词并不是只能下拉选择,而是:[特殊字符] “可选 + 可填”的混合模式(但以规范词为主) 你可以手动输入关键词用于表达新兴方向或细分领域✔ 适用情况:新概念(如“电场调控纳米流体”)交叉领域(如“电流体力学传热耦合”)电场调控电流体力学可以自己填,但必须以系统标准关键词为主,自定义为辅如果你愿意,我可以帮你做一件更关键的事:👉根据你题目,反向设计“最容易中标”的关键词组合 + 学科代码搭配这个比写内容本身还更影响送审质量。你可以手动输入关键词用于表达新兴方向或细分领域✔ 适用情况:新概念(如“电场调控纳米流体”)交叉领域(如“电流体力学传热耦合”)电场调控电流体力学。 阅读详情

相关推荐

机器视觉避坑指南:为什么你的Halcon划痕检测总漏检?(附优化方案)

本文深入剖析了Halcon在表面划痕检测中漏检率高的常见原因,如灰度值波动、噪声干扰及形态学参数设置不当。针对复杂背景与低对比度划痕,提出了超越官方例程的多尺度纹理分析与特征融合优化方案,旨在帮助开发者构建更鲁棒、稳定的机器视觉检测流程,有效提升检出率。

weixin_29057163的博客 243

LeetCode解析------855.考场就座

题目: 在考场里,一排有 N 个座位,分别编号为 0, 1, 2, …, N-1 。 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在 0 号座位上。) 返回 ExamRoom(int N) 类,它有两个公开的函数:其中,函数 ExamRoom.seat() 会返回一个 int...

weixin_44337475的博客 656

RseQC实战指南:从安装到RNA-Seq数据质控全流程解析

本文提供了一份详尽的RseQC实战指南,涵盖从安装配置到RNA-Seq数据质控的全流程。文章重点解析了RseQC作为专业质控工具的核心价值,手把手指导如何通过Conda或pip完成安装,并深入讲解了bam_stat.py、geneBody_coverage.py等关键模块的使用方法与结果解读技巧,帮助用户有效评估RNA完整性、文库构建质量及链特异性,确保下游分析的可靠性。

weixin_29200741的博客 215

leetcode | go | 第855题 | 考场就坐

考场就坐

iCling的博客 553

App inventor2通过蓝牙控制Arduino Uno LED开关

App inventer2控制Arduino Uno LED开关 如果学会这个小程序,大部分的手机通过蓝牙控制Arduino的功能都可以实现,这个小示例是通过蓝牙模块向Arduino发送"0"和"1",而其它功能要发送其它不同的字符串内容给Arduino. 所需硬件: Arduino Uno JDY-16 蓝牙模块(我的是蓝牙4.2版本) 草帽LED(可选) 手机(我用的是安卓,苹果手机应该也可以,最近其应用商店也有App inventor(AI)伴侣) 软件: Aduino IDE或者Platfom

Firmin123456的博客 5514

别再乱点U盘里的.exe了!手把手教你彻底清除那个伪装成Usb Disk的蠕虫病毒

本文详细介绍了如何识别、清除和预防伪装成Usb Disk的蠕虫病毒。从识别感染迹象到安全备份文件,再到深度清理病毒残留和构建防护策略,提供了一套完整的U盘病毒解决方案。特别提醒用户注意U盘病毒的风险,并养成良好的使用习惯。

weixin_30496999的博客 753

LeetCode:855. 考场就座(TreeSet Java)

TreeSet ts:这是一个自定义比较器的TreeSet,用来存储表示座位区间的数组。比较器的逻辑是先比较区间的 “距离”(通过dist方法计算,距离的定义与区间两端的情况有关),如果距离相等则按照区间左端点的大小来排序。其目的是方便快速找到最合适的空闲座位区间来分配座位。

Cosmoshhhyyy的博客 722

【华为OD机试真题】282、求满足条件的最长子串的长度 | 机试真题+思路参考+代码分析 (C++、Java、Py、C语言、JS)

题目描述 >给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度,字符串本身是其最长的子串,子串要求: >  1、只包含1个字母(a~ z,A~Z), 其余必须是数字; 2、字母可以在子 串中的任意位置; >  如果找不到满足要求的子串,如全是字母或全是数字,则返回-1

KJ.JK 1万+

Leetcode855:考场就座

当学生进入考场后,他必须坐在离最近的人最远的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在。个座位排成一行,编号为。设计一个模拟所述考场的类。

m0_67598823的博客 391

【2024/3/12 更新】Windows Server 2019 服务配置与管理汇总

本栏目一共介绍了17种Windows Server 2019 的服务配置与管理,为了方便大家阅读理解,特意总结了17种服务的一些内容。具体说明如下所示

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 2万+

使用C#实现Padim算法的训练和推理:智能缺陷检测的前沿技术

Padim (Patch-based Deep Image Model) 是一种用于无监督异常检测的深度学习算法。它通过提取图像中的局部特征(patch),并在这些特征上构建一个正常样本的分布模型来识别异常。无监督学习:不需要标注数据,适用于缺乏标签的情况。局部特征提取:通过卷积神经网络提取图像的局部特征(patch)。低秩近似:利用低秩近似构建正常样本的协方差矩阵,有效降低计算复杂度。鲁棒性强:能够处理多种类型的缺陷和噪声。

墨夶的博客 1019

LeetCode·每日一题·855.考场就座·有序集合+模拟

有序集合+模拟

m0_64560763的博客 319

Python爬虫实战:高效爬取法律法规数据库的完整指南

本文将详细介绍如何使用Python最新技术栈构建一个高效、稳定的法律法规数据库爬虫。我们将从爬虫的基本原理讲起,逐步深入到反爬机制应对、数据解析与存储、性能优化等高级话题。文章包含完整的代码实现,使用requests-html、aiohttp等现代库,以及应对法律数据库特殊结构的处理技巧。通过本文,您将掌握构建专业级法律数据采集系统的全套技能。关键词:Python爬虫、法律法规、数据采集、异步爬虫、反反爬本文详细介绍了使用Python爬取法律法规数据库的完整技术方案。

2201_76125261的博客 1279

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

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Leetcode.855考场就坐

Leetcode每日一题

感觉画质不如...原神的博客 306

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

855. 考场就座

在考场里,一排有 N 个座位,分别编号为 0, 1, 2, ..., N-1 。当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在 0 号座位上。)返回 ExamRoom(int N) 类,它有两个公开的函数:其中,函数 ExamRoom.seat() 会返回一个 int (整型数...

Do not mess it up 612

[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

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

LeetCode之855_考场就座

本专栏单纯记录自己刷题过程,便于后续重复刷题更新迭代自己对于同一道题的认知和方法。有好的刷题方法和思路,可以评论区交流,感谢~ 1.但凡遇到在动态过程中取最值得要求,肯定要使用有序数据结构,常用的数据结构就是二叉堆和平衡二叉搜索树了。 set<int> s; s.empty() s.insert(0); s.erase(p); 集合(set)。每个元素的值必须唯一;而且系统会根据该值来自动将数据排序;内部结构采用红黑树的平衡二叉树。 class ExamR...

sinat_29634715的博客 470

【LeetCode - 855】考场就座

【中等】设计、有序集合

学哥斌的博客 418

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

Leetcode 855:考场就座

题目描述 在考场里,一排有N个座位,分别编号为0, 1, 2, ..., N-1。 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在 0 号座位上。) 返回ExamRoom(int N)类,它有两个公开的函数:其中,函数ExamRoom.seat()会返回一...

weixin_35338624的博客 514

leetcode-855. 考场就座

思路: 使用set(有序集合)记录seat的位置。 如果set为空,放入索引为0的位置。 遍历set,记录两个人之间距离的一半(pre+这个距离就是中点),如果这个距离比最大距离大,则记录pos和更新最大距离。因为1/2=0。所以将maxDist的初始值设为0。pre设为-1(表示第一元素钱前面没有元素)。pre表示cur前面的那个元素。 最后判断如果放在n-1的位置上,距离是不是会大(n-1-pre>maxDsit),如果大就更新pos。 得到pos后,将pos插入set,返回pos。 cla.

大灰狼吃树皮 271

Exam Room leetcode

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

sysucph的专栏 713

LeetCode 859 Buddy Strings

LeetCode 859 Buddy Strings 传送门 题目分析 Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1: I...

博客 1676
上一篇: LeetCode #1047. Remove All Adjacent Duplicates In String
下一篇: LeetCode #802. Find Eventual Safe States
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址