★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-urzrouss-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Implement a MyCalendarTwo
class to store your events. A new event can be added if adding the event will not cause a triplebooking.git
Your class will have one 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
.github
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 MyCalendar.book
, return true
if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false
and do not add the event to the calendar.app
Your class will be called like this: MyCalendar cal = new MyCalendar();
MyCalendar.book(start, end)
函數
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:this
MyCalendar.book
per test case will be at most 1000
.MyCalendar.book(start, end)
, start
and end
are integers in the range [0, 10^9]
.實現一個 MyCalendar
類來存放你的日程安排。若是要添加的時間內不會致使三重預訂時,則能夠存儲這個新的日程安排。spa
MyCalendar
有一個 book(int start, int end)
方法。它意味着在start到end時間內增長一個日程安排,注意,這裏的時間是半開區間,即 [start, end)
, 實數 x
的範圍爲, start <= x < end
。code
當三個日程安排有一些時間上的交叉時(例如三個日程安排都在同一時間內),就會產生三重預訂。
每次調用 MyCalendar.book
方法時,若是能夠將日程安排成功添加到日曆中而不會致使三重預訂,返回 true
。不然,返回 false
而且不要將該日程安排添加到日曆中。
請按照如下步驟調用MyCalendar
類: MyCalendar cal = new MyCalendar();
MyCalendar.book(start, end)
示例 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)將和第二個日程安排雙重預訂。
說明:
MyCalendar.book
函數最多不超過 100
次。MyCalendar.book(start, end)
時, start
和 end
的取值範圍爲 [0, 10^9]
。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 */
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 }