[Swift]LeetCode729. 個人日程安排表 I | My Calendar I

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ccfpqvww-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.html

Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.node

double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)git

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.github

Your class will be called like this: MyCalendar cal = new MyCalendar();MyCalendar.book(start, end)微信

Example 1:app

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

  • The number of calls to MyCalendar.book per test case will be at most 1000.
  • In calls to MyCalendar.book(start, end)start and end are integers in the range [0, 10^9].

實現一個 MyCalendar 類來存放你的日程安排。若是要添加的時間內沒有其餘安排,則能夠存儲這個新的日程安排。函數

MyCalendar 有一個 book(int start, int end)方法。它意味着在 start 到 end 時間內增長一個日程安排,注意,這裏的時間是半開區間,即 [start, end), 實數 x 的範圍爲,  start <= x < end測試

當兩個日程安排有一些時間上的交叉時(例如兩個日程安排都在同一時間內),就會產生重複預訂。this

每次調用 MyCalendar.book方法時,若是能夠將日程安排成功添加到日曆中而不會致使重複預訂,返回 true。不然,返回 false 而且不要將該日程安排添加到日曆中。

請按照如下步驟調用 MyCalendar 類: MyCalendar cal = new MyCalendar();MyCalendar.book(start, end)

示例 1:

MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
解釋: 
第一個日程安排能夠添加到日曆中.  第二個日程安排不能添加到日曆中,由於時間 15 已經被第一個日程安排預約了。
第三個日程安排能夠添加到日曆中,由於第一個日程安排並不包含時間 20 。

說明:

  • 每一個測試用例,調用 MyCalendar.book 函數最多不超過 100次。
  • 調用函數 MyCalendar.book(start, end)時, start 和 end 的取值範圍爲 [0, 10^9]

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  */
相關文章
相關標籤/搜索