-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_calendar_iii.go
56 lines (48 loc) · 1.5 KB
/
my_calendar_iii.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
package problem0732
import "sort"
/*
A k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events.)
You are given some events [start, end), after each given event, return an integer k representing the maximum k-booking between all the previous events.
Implement the MyCalendarThree class:
MyCalendarThree() Initializes the object.
int book(int start, int end) Returns an integer k representing the largest integer such that there exists a k-booking in the calendar.
*/
type MyCalendarThree struct {
// Using a map and a slice to mimic an ordered map
Events map[int]int
Order []int
}
func Constructor() MyCalendarThree {
return MyCalendarThree{
Events: make(map[int]int),
Order: make([]int, 0),
}
}
func (this *MyCalendarThree) Book(start int, end int) int {
var ongoing, res int
// Adding event start to events
if _, ok := this.Events[start]; !ok {
this.Order = InsertSorted(this.Order, start)
}
this.Events[start]++ //When event starts, add 1
// Adding event end to events
if _, ok := this.Events[end]; !ok {
this.Order = InsertSorted(this.Order, end)
}
this.Events[end]-- //When event ends, subtract 1
for _, key := range this.Order {
ongoing += this.Events[key]
if ongoing > res {
res = ongoing
}
}
return res
}
// Inserts a number into a sorted list
func InsertSorted(list []int, val int) []int {
idx := sort.SearchInts(list, val)
list = append(list, 0)
copy(list[idx+1:], list[idx:])
list[idx] = val
return list
}