首页 > 其他 > 详细

[LeetCode] Exam Room 考试房间

时间:2019-03-28 23:07:50      阅读:284      评论:0      收藏:0      [点我收藏+]



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.



参考资料:

https://leetcode.com/problems/exam-room/

[LeetCode] Exam Room 考试房间

原文:https://www.cnblogs.com/grandyang/p/10618437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!