|
| 1 | +package leetcode |
| 2 | + |
| 3 | +// 解法一 二叉排序树 |
| 4 | +// Event define |
| 5 | +type Event struct { |
| 6 | + start, end int |
| 7 | + left, right *Event |
| 8 | +} |
| 9 | + |
| 10 | +// Insert define |
| 11 | +func (e *Event) Insert(curr *Event) bool { |
| 12 | + if e.end > curr.start && curr.end > e.start { |
| 13 | + return false |
| 14 | + } |
| 15 | + if curr.start < e.start { |
| 16 | + if e.left == nil { |
| 17 | + e.left = curr |
| 18 | + } else { |
| 19 | + return e.left.Insert(curr) |
| 20 | + } |
| 21 | + } else { |
| 22 | + if e.right == nil { |
| 23 | + e.right = curr |
| 24 | + } else { |
| 25 | + return e.right.Insert(curr) |
| 26 | + } |
| 27 | + } |
| 28 | + return true |
| 29 | +} |
| 30 | + |
| 31 | +// MyCalendar define |
| 32 | +type MyCalendar struct { |
| 33 | + root *Event |
| 34 | +} |
| 35 | + |
| 36 | +// Constructor729 define |
| 37 | +func Constructor729() MyCalendar { |
| 38 | + return MyCalendar{ |
| 39 | + root: nil, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Book define |
| 44 | +func (this *MyCalendar) Book(start int, end int) bool { |
| 45 | + curr := &Event{start: start, end: end, left: nil, right: nil} |
| 46 | + if this.root == nil { |
| 47 | + this.root = curr |
| 48 | + return true |
| 49 | + } |
| 50 | + return this.root.Insert(curr) |
| 51 | +} |
| 52 | + |
| 53 | +// 解法二 快排 + 二分 |
| 54 | +// MyCalendar define |
| 55 | +// type MyCalendar struct { |
| 56 | +// calendar []Interval |
| 57 | +// } |
| 58 | + |
| 59 | +// // Constructor729 define |
| 60 | +// func Constructor729() MyCalendar { |
| 61 | +// calendar := []Interval{} |
| 62 | +// return MyCalendar{calendar: calendar} |
| 63 | +// } |
| 64 | + |
| 65 | +// // Book define |
| 66 | +// func (this *MyCalendar) Book(start int, end int) bool { |
| 67 | +// if len(this.calendar) == 0 { |
| 68 | +// this.calendar = append(this.calendar, Interval{Start: start, End: end}) |
| 69 | +// return true |
| 70 | +// } |
| 71 | +// // 快排 |
| 72 | +// quickSort(this.calendar, 0, len(this.calendar)-1) |
| 73 | +// // 二分 |
| 74 | +// pos := searchLastLessInterval(this.calendar, start, end) |
| 75 | +// // 如果找到最后一个元素,需要判断 end |
| 76 | +// if pos == len(this.calendar)-1 && this.calendar[pos].End <= start { |
| 77 | +// this.calendar = append(this.calendar, Interval{Start: start, End: end}) |
| 78 | +// return true |
| 79 | +// } |
| 80 | +// // 如果不是开头和结尾的元素,还需要判断这个区间是否能插入到原数组中(要看起点和终点是否都能插入) |
| 81 | +// if pos != len(this.calendar)-1 && pos != -1 && this.calendar[pos].End <= start && this.calendar[pos+1].Start >= end { |
| 82 | +// this.calendar = append(this.calendar, Interval{Start: start, End: end}) |
| 83 | +// return true |
| 84 | +// } |
| 85 | +// // 如果元素比开头的元素还要小,要插入到开头 |
| 86 | +// if this.calendar[0].Start >= end { |
| 87 | +// this.calendar = append(this.calendar, Interval{Start: start, End: end}) |
| 88 | +// return true |
| 89 | +// } |
| 90 | +// return false |
| 91 | +// } |
| 92 | + |
| 93 | +// func searchLastLessInterval(intervals []Interval, start, end int) int { |
| 94 | +// low, high := 0, len(intervals)-1 |
| 95 | +// for low <= high { |
| 96 | +// mid := low + ((high - low) >> 1) |
| 97 | +// if intervals[mid].Start <= start { |
| 98 | +// if (mid == len(intervals)-1) || (intervals[mid+1].Start > start) { // 找到最后一个小于等于 target 的元素 |
| 99 | +// return mid |
| 100 | +// } |
| 101 | +// low = mid + 1 |
| 102 | +// } else { |
| 103 | +// high = mid - 1 |
| 104 | +// } |
| 105 | +// } |
| 106 | +// return -1 |
| 107 | +// } |
| 108 | + |
| 109 | +/** |
| 110 | + * Your MyCalendar object will be instantiated and called as such: |
| 111 | + * obj := Constructor(); |
| 112 | + * param_1 := obj.Book(start,end); |
| 113 | + */ |
0 commit comments