-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
49 lines (41 loc) · 835 Bytes
/
main.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
package problem21
import (
"container/heap"
"sort"
)
func Solution(intervals [][]int) int {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
h := &IntHeap{}
heap.Init(h)
count := 0
for _, interval := range intervals {
if h.Len() == 0 {
count++
heap.Push(h, interval[1])
} else {
if interval[0] >= (*h)[0] {
heap.Pop(h)
} else {
count++
}
heap.Push(h, interval[1])
}
}
return count
}
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x any) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}