在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Implement a Your class will have the method, A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both 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(15, 25); // returns false MyCalendar.book(20, 30); // returns true Explanation: The first event can be booked. The second can't because time 15 is already booked by another event. The third event can be booked, as the first event takes every time less than 20, but not including 20. Note:
实现一个
当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生重复预订。 每次调用 请按照以下步骤调用 示例 1: MyCalendar(); MyCalendar.book(10, 20); // returns true MyCalendar.book(15, 25); // returns false MyCalendar.book(20, 30); // returns true 解释: 第一个日程安排可以添加到日历中. 第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了。 第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20 。 说明:
552ms 1 class MyCalendar { 2 var root: Node? 3 init() {} 4 5 func book(_ start: Int, _ end: Int) -> Bool { 6 if self.root == nil{ 7 self.root = Node(start, end) 8 return true 9 } 10 return self.root?.insert(start: start, end: end) ?? false 11 } 12 } 13 14 class Node { 15 var start = 0 16 var end = 0 17 var smallLeft: Node? 18 var bigRight: Node? 19 init(_ start:Int, _ end:Int) { 20 self.start = start 21 self.end = end 22 } 23 unc insert(start:Int, end:Int) -> Bool { 24 if end <= self.start { 25 if self.smallLeft == nil { 26 self.smallLeft = Node(start, end) 27 return true 28 } 29 return self.smallLeft?.insert(start: start, end: end) ?? false 30 } 31 else if start >= self.end { 32 if self.bigRight == nil { 33 self.bigRight = Node(start, end) 34 return true 35 } 36 return self.bigRight?.insert(start: start, end: end) ?? false 37 } 38 return false 39 } 40 } 41 /** 42 * Your MyCalendar object will be instantiated and called as such: 43 * let obj = MyCalendar() 44 * let ret_1: Bool = obj.book(start, end) 45 */ 556ms 1 class MyCalendar { 2 var root: Node? 3 4 init() { 5 root = nil 6 } 7 8 func book(_ start: Int, _ end: Int) -> Bool { 9 var event = Event.init(start, end) 10 if (root == nil) { 11 root = Node.init(event) 12 return true 13 } 14 return root!.insert(root!, event) 15 } 16 } 17 18 struct Event { 19 var start: Int = 0 20 var end: Int = 0 21 22 init(_ start: Int, _ end: Int) { 23 self.start = start 24 self.end = end 25 } 26 27 func overlaps(_ input: Event)-> Bool { 28 if (self.end <= input.start) || (self.start >= input.end) { 29 return true 30 } 31 else { 32 return false 33 } 34 } 35 } 36 37 class Node { 38 var event: Event 39 var left: Node? 40 var right: Node? 41 42 init(_ event: Event) { 43 self.event = event 44 self.left = nil 45 self.right = nil 46 } 47 48 func insert(_ node: Node, _ event: Event)-> Bool { 49 if (event.end <= node.event.start) { 50 if node.left == nil { 51 node.left = Node.init(event) 52 return true 53 } 54 else { 55 return insert(node.left!, event) 56 } 57 } 58 else if (event.start >= node.event.end) { 59 if (node.right == nil) { 60 node.right = Node.init(event) 61 return true 62 } 63 else { 64 return insert(node.right!, event) 65 } 66 } 67 else { 68 return false 69 } 70 } 71 } 564ms 1 class MyCalendar { 2 var tree = Tree() 3 4 init() { 5 6 } 7 8 func book(_ start: Int, _ end: Int) -> Bool { 9 guard start >= 0, end >= 0, end > start else { 10 return false 11 } 12 13 let node = Node((start,end)) 14 return tree.insert(node) 15 } 16 } 17 18 class Node { 19 var start: Int 20 var end: Int 21 var left: Node? 22 var right: Node? 23 init(_ book: (Int, Int)) { 24 start = book.0 25 end = book.1 26 } 27 } 28 29 class Tree { 30 var root: Node? 31 32 func insert(_ node: Node, at root: Node) -> Bool { 33 if node.start >= root.start && node.start < root.end { 34 return false 35 } 36 37 if node.end <= root.start, let left = root.left { 38 return insert(node, at: left) 39 } else if node.start >= root.end, let right = root.right { 40 return insert(node, at: right) 41 } else { 42 if node.end <= root.start { 43 root.left = node 44 return true 45 } 46 47 if node.start >= root.end { 48 root.right = node 49 return true 50 } 51 } 52 return false 53 } 54 55 func insert(_ node: Node) -> Bool { 56 guard let root = self.root else { 57 self.root = node 58 return true 59 } 60 return insert(node, at: root) 61 } 62 } Runtime: 1104 ms
Memory Usage: 20.1 MB
1 class MyCalendar { 2 var cal:[(Int,Int)] 3 4 init() { 5 cal = [(Int,Int)]() 6 } 7 8 func book(_ start: Int, _ end: Int) -> Bool { 9 for val in cal 10 { 11 if max(val.0, start) < min(val.1, end) 12 { 13 return false 14 } 15 } 16 cal.append((start, end)) 17 return true 18 } 19 } 20 /** 21 * Your MyCalendar object will be instantiated and called as such: 22 * let obj = MyCalendar() 23 * let ret_1: Bool = obj.book(start, end) 24 */
|
请发表评论