-
Notifications
You must be signed in to change notification settings - Fork 373
/
conflict_appointment_test.go
68 lines (57 loc) · 1.39 KB
/
conflict_appointment_test.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
/*
Problem:
- Given a list of intervals, check if any of them is conflicting.
Example:
- Input: []interval{{1, 2}, {2, 3}, {4, 5}}
Output: false
- Input: []interval{{1, 5}, {2, 3}}
Output: true
Approach:
- Similar to merge intervals problem, need to return the true
immediately if any of them is conflicting.
Cost:
- O(nlogn) time, O(n) space.
*/
package gtci
import (
"sort"
"testing"
"github.com/hoanhan101/algo/common"
)
func TestIsConflicting(t *testing.T) {
tests := []struct {
in []interval
expected bool
}{
{[]interval{}, false},
{[]interval{{1, 2}}, false},
{[]interval{{1, 2}, {2, 3}}, false},
{[]interval{{2, 3}, {1, 2}}, false},
{[]interval{{1, 2}, {2, 3}, {5, 6}}, false},
{[]interval{{1, 2}, {5, 6}, {2, 3}}, false},
{[]interval{{1, 2}, {1, 3}}, true},
{[]interval{{1, 3}, {1, 3}}, true},
{[]interval{{1, 2}, {2, 3}, {2, 6}}, true},
{[]interval{{1, 2}, {2, 6}, {2, 3}}, true},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
isConflicting(tt.in),
)
}
}
func isConflicting(intervals []interval) bool {
// sort the intervals in ascending order.
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].start < intervals[j].start
})
// return false immediately if any interval is conflicting.
for i := 1; i < len(intervals); i++ {
if intervals[i].start < intervals[i-1].end {
return true
}
}
return false
}