在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In an exam room, there are 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 Example 1: Input: [[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:
在考场里,一排有 当学生进入考场后,他必须坐在能够使他与离他最近的人之间的距离达到最大化的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在 0 号座位上。) 返回 示例: 输入:["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 号座位上。 提示:
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 */
|
请发表评论