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