-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.go
46 lines (39 loc) · 1.05 KB
/
solution.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
type MyCalendarTwo struct {
single []Interval
doubleBooked []Interval
}
type Interval struct {
start, end int
}
func Constructor() MyCalendarTwo {
return MyCalendarTwo{}
}
func (this *MyCalendarTwo) Book(start int, end int) bool {
// Check for triple booking by overlapping with double booked intervals
for _, booking := range this.doubleBooked {
if max(start, booking.start) < min(end, booking.end) {
return false // Triple booking detected
}
}
// Add overlapping parts to double bookings
for _, booking := range this.single {
if max(start, booking.start) < min(end, booking.end) {
this.doubleBooked = append(this.doubleBooked, Interval{max(start, booking.start), min(end, booking.end)})
}
}
// Add the event to single bookings
this.single = append(this.single, Interval{start, end})
return true
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}