[Swift]LeetCode855. 考场就座 | Exam Room
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10594220.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 <= N <= 10^9
ExamRoom.seat()
andExamRoom.leave()
will be called at most10^4
times across all test cases.- Calls to
ExamRoom.leave(p)
are guaranteed to have a student currently sitting in seat numberp
.
在考场里,一排有 N
个座位,分别编号为 0, 1, 2, ..., N-1
。
当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在 0 号座位上。)
返回 ExamRoom(int N)
类,它有两个公开的函数:其中,函数 ExamRoom.seat()
会返回一个 int
(整型数据),代表学生坐的位置;函数 ExamRoom.leave(int p)
代表坐在座位 p
上的学生现在离开了考场。请确保每次调用 ExamRoom.leave(p)
时都有学生坐在座位 p
上。
示例:
输入:["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]] 输出:[null,0,9,4,2,null,5] 解释: ExamRoom(10) -> null seat() -> 0,没有人在考场里,那么学生坐在 0 号座位上。 seat() -> 9,学生最后坐在 9 号座位上。 seat() -> 4,学生最后坐在 4 号座位上。 seat() -> 2,学生最后坐在 2 号座位上。 leave(4) -> null seat() -> 5,学生最后坐在 5 号座位上。
提示:
1 <= N <= 10^9
- 在所有的测试样例中
ExamRoom.seat()
和ExamRoom.leave()
最多被调用10^4
次。 - 调用
ExamRoom.leave(p)
时需要确保当前有学生坐在座位p
上。
336ms
1 class ExamRoom { 2 var students: [Int] 3 let N: Int 4 5 init(_ N: Int) { 6 students = [Int]() 7 self.N = N 8 } 9 10 // with sort it's O(PlogP), with index it's O(P) 11 func seat() -> Int { 12 var student = 0 13 var index = 0 14 if students.count > 0 { 15 var prev: Int? = nil 16 var dist = students.first ?? 0 17 for (i, studentPos) in students.enumerated() { 18 if let prev = prev { 19 let d: Int = (studentPos - prev) / 2 20 if d > dist { 21 dist = d 22 student = prev + d 23 index = i 24 } 25 } 26 prev = studentPos 27 } 28 if N - 1 - (students.last ?? 0) > dist { 29 student = N - 1 30 index = students.count 31 } 32 } 33 students.insert(student, at: index) 34 return student 35 } 36 37 // O(P), because of remove. 38 func leave(_ p: Int) { 39 for i in 0..<students.count { 40 if students[i] == p { 41 students.remove(at: i) 42 return 43 } 44 } 45 } 46 } 47 48 // wrong 49 class ExamRoom1 { 50 51 var seats: [Bool] 52 53 init(_ N: Int) { 54 seats = Array(repeating: false, count: N) 55 } 56 57 func seat() -> Int { 58 let index = findSeat(0, seats.count - 1) 59 seats[index] = true 60 return index 61 } 62 63 func leave(_ p: Int) { 64 seats[p] = false 65 } 66 67 private func findSeat(_ l: Int, _ r: Int) -> Int { 68 if seats[l] == false { 69 return 0 70 } 71 if seats[r] == false { 72 return r 73 } 74 let m = (l + r) / 2 75 if seats[m] == false { 76 return m 77 } 78 let lh = findSeat(l, m) 79 if seats[lh] == false { 80 return lh 81 } 82 83 let rh = findSeat(m, r) 84 if seats[rh] == false { 85 return rh 86 } 87 return -1 88 } 89 } 90 91 /** 92 * Your ExamRoom object will be instantiated and called as such: 93 * let obj = ExamRoom(N) 94 * let ret_1: Int = obj.seat() 95 * obj.leave(p) 96 */
1 class ExamRoom { 2 static var N:Int = 0 3 var pq:[Interval] = [Interval]() 4 5 init(_ N: Int) { 6 ExamRoom.N = N 7 pq.append(Interval(-1, N)) 8 } 9 10 func seat() -> Int { 11 var seat:Int = 0 12 var interval:Interval = pq.removeFirst() 13 if interval.x == -1 {seat = 0} 14 else if interval.y == ExamRoom.N {seat = ExamRoom.N - 1} 15 else {seat = (interval.x + interval.y) / 2} 16 offer(Interval(interval.x, seat)) 17 offer(Interval(seat, interval.y)) 18 return seat 19 } 20 21 func leave(_ p: Int) { 22 var head:Interval? = nil 23 var tail:Interval? = nil 24 for interval in pq 25 { 26 if interval.x == p {tail = interval} 27 if interval.y == p {head = interval} 28 if head != nil && tail != nil {break} 29 } 30 removeEle(head!) 31 removeEle(tail!) 32 offer(Interval(head!.x, tail!.y)) 33 } 34 35 func removeEle(_ interval:Interval) 36 { 37 for i in (0..<pq.count).reversed() 38 { 39 if pq[i].x == interval.x && pq[i].y == interval.y && pq[i].dist == interval.dist 40 { 41 pq.remove(at:i) 42 } 43 } 44 } 45 46 func offer(_ interval:Interval) 47 { 48 pq.append(interval) 49 pq.sort(by:{(a:Interval,b:Interval) -> Bool in 50 if a.dist == b.dist 51 { 52 return a.x <= b.x 53 } 54 else 55 { 56 return a.dist > b.dist 57 } 58 }) 59 } 60 } 61 62 class Interval{ 63 var x:Int 64 var y:Int 65 var dist:Int 66 init(_ x:Int,_ y:Int) 67 { 68 self.x = x 69 self.y = y 70 if x == -1 71 { 72 self.dist = y 73 } 74 else if y == ExamRoom.N 75 { 76 self.dist = ExamRoom.N - 1 - x 77 } 78 else 79 { 80 self.dist = abs(x - y) / 2 81 } 82 } 83 } 84 85 /** 86 * Your ExamRoom object will be instantiated and called as such: 87 * let obj = ExamRoom(N) 88 * let ret_1: Int = obj.seat() 89 * obj.leave(p) 90 */
转载于:https://www.cnblogs.com/strengthen/p/10594220.html
[Swift]LeetCode855. 考场就座 | Exam Room相关推荐
- leetcode855. 考场就座
题目地址 https://leetcode-cn.com/problems/exam-room/ 题目描述 在考场里,一排有 N 个座位,分别编号为 0, 1, 2, ..., N-1 . 当学生进入 ...
- leetcode-855. 考场就座
思路: 使用set(有序集合)记录seat的位置. 如果set为空,放入索引为0的位置. 遍历set,记录两个人之间距离的一半(pre+这个距离就是中点),如果这个距离比最大距离大,则记录pos和更新 ...
- Leetcode 855. Exam Room 考场就座:提供两种解法
Leetcode 855. Exam Room 考场就座: 提供两种解法 855. Exam Room 考场就座(两种解法) 题目描述 示例: 解答1 代码1 解答2 代码2 855. Exam Ro ...
- LeetCode 855. 考场就座
855. 考场就座 [有序集合]用TreeSet存已经选好的座位,每次遍历所有的座位,如果两个之间的差值比当前max大,那么更新max和最后落座的位置.需要特别注意特判一下开头和结尾,因为有leave ...
- 855. 考场就座(高频题)
855. 考场就座 题目 解题思路 代码 题目 在考场里,一排有 N 个座位,分别编号为 0, 1, 2, -, N-1 . 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座 ...
- LeetCode - OrderMap - 855.考场就座
题目 855.考场就座 难度 中等 在考场里,一排有 N 个座位,分别编号为 0, 1, 2, -, N-1 . 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上.如果有 ...
- LeetCode解析------855.考场就座
题目: 在考场里,一排有 N 个座位,分别编号为 0, 1, 2, -, N-1 . 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上.如果有多个这样的座位,他会坐在编号 ...
- Exam Room 考场就座
在考场里,一排有 N 个座位,分别编号为 0, 1, 2, ..., N-1 . 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上.如果有多个这样的座位,他会坐在编号最小 ...
- LeetCode855:考场就座
如题: 思路分析: 我们每次挑选座位的时候,需要里最近的人距离最大,也就是说如果要坐在两个人之间,应该坐在中点的位置 因此我们可以将两个座位抽象成一个线段,第一个人是线段开始位置start,第二个人是 ...
最新文章
- Java8 ConcurrentHashMap详解
- 回调函数之Java/C++版本
- 一加nfc门禁卡录入_忘记门禁卡不再徘徊 一加7T多功能NFC过来拯救你
- 海思芯片怎么使用tde给qt加速_基于Hisi芯片,交叉编译、移植Qt4.8.6(可旋转)
- python网课期中答案_中国大学python编程答案网课MOOC考试期末慕课答案
- 云服务器架设网站教程_阿里云服务器购买流程详细教程及注意事项
- VS2012+Win7网站发布详细步骤
- [转]vs2010 crystal report使用
- python下载网易云音乐付费歌曲有哪些_Python3批量下载网易云音乐热歌榜
- java用于输入输出流的类_java输入输出流
- 什么是java句柄_JAVA中的术语:句柄是啥意思
- python 爬虫保存为word_微信公众号文章爬虫,本地word文档保存
- 自动控制原理分析工作原理以及方框图做题过程
- linux平台使用yum安装mysql
- logback配置 (分文件夹、可配路径)_hanCSDN_20180906
- 滤色,颜色减淡,正片叠底
- 服务器文件夹怎么用快捷方式打开,文件夹变成快捷方式怎么办 文件夹变成快捷方式解决方法...
- 反弹中快速获利的操盘技巧
- 解题记录 LeetCode 下一个更大元素 单调栈
- LM393芯片功能及原理
热门文章
- springboot项目导出excel 合并单元格表格
- 张江陵怎么从机械到计算机的,2014湖北省大学研究生院排行出炉 武汉大学居榜首...
- linux swift开发环境,Linux平台swift语言开发学习环境搭建
- swift 隐藏状态栏_swift 同时修改状态栏和导航栏出现的问题及解决的方法
- 使用MyEntunnel和Proxifier搭建代理服务器
- Qt对话框的事件循环分析(子线程中不能创建UI窗体分析)
- 要怎么通过PHP发布微博动态:附代码详解
- python函数测试_python绘制评估优化算法性能的测试函数
- l28n和开发版_*** am335x开发板的疑问以及解答
- CSS书写规范和顺序