在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Implement a Your class will have one method, A triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.) For each call to the method Your class will be called like this: Example 1: MyCalendar(); MyCalendar.book(10, 20); // returns true MyCalendar.book(50, 60); // returns true MyCalendar.book(10, 40); // returns true MyCalendar.book(5, 15); // returns false MyCalendar.book(5, 10); // returns true MyCalendar.book(25, 55); // returns true Explanation: The first two events can be booked. The third event can be double booked. The fourth event (5, 15) can't be booked, because it would result in a triple booking. The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked. The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event; the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event. Note:
实现一个
当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生三重预订。 每次调用 请按照以下步骤调用 示例 1: MyCalendar(); MyCalendar.book(10, 20); // returns true MyCalendar.book(50, 60); // returns true MyCalendar.book(10, 40); // returns true MyCalendar.book(5, 15); // returns false MyCalendar.book(5, 10); // returns true MyCalendar.book(25, 55); // returns true 解释: 前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。 第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。 第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。 第六个日程安排(25,55)可以添加至日历中,因为时间 [25,40] 将和第三个日程安排双重预订; 时间 [40,50] 将单独预订,时间 [50,55)将和第二个日程安排双重预订。 说明:
728ms 1 class MyCalendarTwo { 2 typealias Interval = (start: Int, end: Int) 3 var intervals: [Interval] = [] 4 var overlaps: [Interval] = [] 5 init() { 6 7 } 8 9 func book(_ start: Int, _ end: Int) -> Bool { 10 for overlap in overlaps { 11 if overlap.start < end, overlap.end > start { 12 return false 13 } 14 } 15 for interval in intervals { 16 if interval.start < end, interval.end > start { 17 overlaps.append((max(interval.start, start), min(interval.end, end))) 18 } 19 } 20 intervals.append((start, end)) 21 22 return true 23 } 24 } 25 /** 26 * Your MyCalendarTwo object will be instantiated and called as such: 27 * let obj = MyCalendarTwo() 28 * let ret_1: Bool = obj.book(start, end) 29 */ Runtime: 748 ms
Memory Usage: 20.1 MB
1 class MyCalendarTwo { 2 var s1:[(Int,Int)] 3 var s2:[(Int,Int)] 4 5 init() { 6 s1 = [(Int,Int)]() 7 s2 = [(Int,Int)]() 8 } 9 10 func book(_ start: Int, _ end: Int) -> Bool { 11 for ele in s2 12 { 13 if start >= ele.1 || end <= ele.0 14 { 15 continue 16 } 17 else 18 { 19 return false 20 } 21 } 22 for ele in s1 23 { 24 if start >= ele.1 || end <= ele.0 25 { 26 continue 27 } 28 else 29 { 30 s2.append((max(start, ele.0), min(end, ele.1))) 31 } 32 } 33 s1.append((start, end)) 34 return true; 35 } 36 } 37 38 /** 39 * Your MyCalendarTwo object will be instantiated and called as such: 40 * let obj = MyCalendarTwo() 41 * let ret_1: Bool = obj.book(start, end) 42 */ 852ms 1 class MyCalendarTwo { 2 var books = [(start: Int, end: Int)]() 3 var doubles = [(start: Int, end: Int)]() 4 5 func book(_ start: Int, _ end: Int) -> Bool { 6 7 for double in doubles { 8 if start<double.end && end>double.start { 9 return false 10 } 11 } 12 13 for book in books { 14 if start<book.end && end>book.start { 15 let double = (max(start, book.start),min(end, book.end)) 16 doubles.append(double) 17 } 18 } 19 20 books.append((start,end)) 21 return true 22 } 23 } 1596ms 1 import Foundation 2 3 class MyCalendarTwo { 4 5 public var events1: [(Int, Int)] 6 public var events2: [(Int, Int)] 7 8 init() { 9 events1 = [(Int, Int)]() 10 events2 = [(Int, Int)]() 11 } 12 13 func book(_ start: Int, _ end: Int) -> Bool { 14 guard start < end else { return false } 15 guard start >= 0 && start <= 1000000000 else { return false } 16 guard end >= 0 && end <= 1000000000 else { return false } 17 18 var newEvent = (start, end) 19 // backup 20 let _events1 = events1 21 let _events2 = events2 22 23 var offset = 0; 24 25 // Check events1 26 for (i, event) in events1.enumerated() { 27 // left, prepend 28 if newEvent.1 <= event.0 { 29 events1.insert(newEvent, at: i + offset) 30 offset += 1 31 return true 32 } 33 34 // right 35 if newEvent.0 >= event.1 { continue } 36 37 // intersection 38 if newEvent.0 < event.0 { 39 // partial, insert left part 40 events1.insert((newEvent.0, event.0), at: i + offset) 41 offset += 1 42 newEvent.0 = event.0 43 } 44 45 if newEvent.1 <= event.1 { 46 // overlap, check all, double book 47 guard book2(newEvent) else { 48 events1 = _events1 49 events2 = _events2 50 return false 51 } 52 return true 53 } else { 54 // partial, check left part, double book 55 guard book2(newEvent.0, event.1) else { 56 events1 = _events1 57 events2 = _events2 58 return false 59 } 60 newEvent.0 = event.1 61 } 62 } 63 64 // no insertion, append to the last 65 events1.append(newEvent) 66 return true 67 } 68 69 private func book2(_ start: Int, _ end: Int) -> Bool { 70 guard start < end else { return false } 71 guard start >= 0 && start <= 1000000000 else { return false } 72 guard end >= 0 && end <= 1000000000 else { return false } 73 74 let newEvent = (start, end) 75 76 return book2(newEvent) 77 } 78 79 private func book2(_ newEvent: (Int, Int)) -> Bool { 80 for (i, event) in events2.enumerated() { 81 guard newEvent.0 >= event.1 || newEvent.1 <= event.0 else { return false } 82 83 // left, prepend 84 if newEvent.1 <= event.0 { 85 events2.insert(newEvent, at: i) 86 return true 87 } 88 89 // right 90 if newEvent.0 >= event.1 { continue } 91 } 92 93 events2.append(newEvent) 94 return true 95 } 96 }
|
请发表评论