-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1229.go
49 lines (43 loc) · 883 Bytes
/
1229.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
func minAvailableDuration(A [][]int, B [][]int, duration int) []int {
sort.Sort(IntSlice(A))
sort.Sort(IntSlice(B))
i, j := 0, 0
for i < len(A) && j < len(B) {
l := max(A[i][0], B[j][0])
r := min(A[i][1], B[j][1])
if l + duration <= r {
return []int{l, l + duration};
}
if A[i][1] < B[j][1] {
i++
} else {
j++
}
}
return []int{}
}
type IntSlice [][]int
func (s IntSlice) Len() int {
return len(s)
}
func (s IntSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s IntSlice) Less(i, j int) bool {
if s[i][0] != s[j][0] {
return s[i][0] < s[j][0]
}
return s[i][1] < s[j][1]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}