ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

855. Exam Room

2021-06-06 15:02:33  阅读:217  来源: 互联网

标签:855 last Exam number 距离 seat 座位 sits Room


问题:

设计类:

给定N个座位。

入座:seat:给当前进入的考生,安排最远距离座位,返回座位号。

离开:leave(p):座位号为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.

  

解法:SystemDesign

动态添加元素,选择最大距离位置添加。

这种问题,本来使用已排序功能的数据结构set or map 都比较好。

但由于comparator的构造比较困难,涉及到业务变量N的参照,本次未使用该方法。

 

思路:

⚠️ 注意:两端的座位,与中间座位的距离判断不同。

  • 两端距离=
    • 位置0: 距离第一个位置的距离:L[0]
    • 位置N-1(末尾位置):距离最后一个位置的距离:N-1-L[last]
  • 中间座位距离maxdis=
    • (L[i]-L[i-1])/2
      • 若取该中间位置,则新pos=
      • (L[i-1]+maxdis) 或者 (L[i]+L[i-1])/2

 

逻辑:

  • 入座:
    • 寻找所有座位L之间的最大距离maxdis
    • 再遍历找到最大距离者,进行入座。
  • 离开:
    • 找到所有座位L中==p的位置,删除。

 

 

代码参考:

 1 class ExamRoom {
 2 private:
 3     int N;
 4     vector<int> L;
 5 public:    
 6     ExamRoom(int n) {
 7         N=n;
 8     }
 9     
10     int seat() {
11         if(L.empty()) {
12             L.push_back(0);
13             return 0;
14         }
15         int maxdis = max(L[0], N-1-L[L.size()-1]);//get two bound distance.
16         //find maxdis
17         for(int i=1; i<L.size(); i++) maxdis = max(maxdis, (L[i]-L[i-1])/2);
18         
19         //insert p at maxdis
20         //test pos:0
21         if(maxdis==L[0]) {
22             L.insert(L.begin(), 0);
23             return 0;
24         }
25         //test middle pos
26         for(int i=1; i<L.size(); i++) {
27             if((L[i]-L[i-1])/2 == maxdis) {
28                 L.insert(L.begin()+i, L[i-1]+maxdis);
29                 return L[i];
30             }
31         }
32         //test pos:N-1
33         L.push_back(N-1);
34         return N-1;
35     }
36     
37     void leave(int p) {
38         for(int i=0; i<L.size(); i++) {
39             if(L[i] == p) L.erase(L.begin()+i);
40         }
41     }
42 };
43 
44 /**
45  * Your ExamRoom object will be instantiated and called as such:
46  * ExamRoom* obj = new ExamRoom(n);
47  * int param_1 = obj->seat();
48  * obj->leave(p);
49  */

 

标签:855,last,Exam,number,距离,seat,座位,sits,Room
来源: https://www.cnblogs.com/habibah-chang/p/14855321.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有