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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-dklvcyjc-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1.git

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

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:app

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.

在考场里,一排有 N 个座位,分别编号为 0, 1, 2, ..., N-1 。测试

当学生进入考场后,他必须坐在可以使他与离他最近的人之间的距离达到最大化的座位上。若是有多个这样的座位,他会坐在编号最小的座位上。(另外,若是考场里没有人,那么学生就坐在 0 号座位上。)spa

返回 ExamRoom(int N) 类,它有两个公开的函数:其中,函数 ExamRoom.seat() 会返回一个 int (整型数据),表明学生坐的位置;函数 ExamRoom.leave(int p) 表明坐在座位 p上的学生如今离开了考场。请确保每次调用 ExamRoom.leave(p) 时都有学生坐在座位 p 上。 code

示例:htm

输入:["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. 1 <= N <= 10^9
  2. 在全部的测试样例中 ExamRoom.seat() 和 ExamRoom.leave() 最多被调用 10^4 次。
  3. 调用 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  */ 

Runtime: 2084 ms
Memory Usage: 20.1 MB
 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  */ 
相关文章
相关标签/搜索