-
Notifications
You must be signed in to change notification settings - Fork 0
/
meeting_rooms_sort_heap_medium.go
84 lines (70 loc) · 1.96 KB
/
meeting_rooms_sort_heap_medium.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 253. Meeting Rooms II
// https://leetcode.com/problems/meeting-rooms-ii/
// Sort by starting time. Maintain BST with ending time being the priority.
// First ending being removed and the new one comes.
// Maximum size of the priority queue is the required number of meeting rooms.
package main
import (
"container/heap"
"log"
"sort"
)
func main() {
// log.Println(minMeetingRooms([][]int{[]int{0, 30}, []int{5, 10}, []int{15, 20}}))
// log.Println(minMeetingRooms([][]int{[]int{26, 29}, []int{19, 26}, []int{19, 28}, []int{4, 19}, []int{4, 25}})) //3
// log.Println(minMeetingRooms([][]int{[]int{2, 15}, []int{36, 45}, []int{9, 29}, []int{16, 23}, []int{4, 9}})) //2
// log.Println(minMeetingRooms([][]int{[]int{6, 17}, []int{8, 9}, []int{11, 12}, []int{6, 9}})) //3
}
type Meetings [][]int
func (m Meetings) Len() int {
return len(m)
}
func (m Meetings) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m Meetings) Less(i, j int) bool {
return m[i][0] < m[j][0]
}
type Item struct {
start int
priority int
}
// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].priority < pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
*pq = append(*pq, x.(*Item))
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
*pq = old[0 : n-1]
return item
}
func minMeetingRooms(intervals [][]int) int {
mm := Meetings(intervals)
sort.Sort(mm)
pq := make(PriorityQueue, 0)
heap.Init(&pq)
log.Println(mm)
max := len(pq)
for i := 0; i < len(intervals); i++ {
for len(pq) > 0 && pq[0].priority <= intervals[i][0] {
_ = heap.Pop(&pq)
}
item := Item{intervals[i][0], intervals[i][1]}
heap.Push(&pq, &item)
if len(pq) > max {
max = len(pq)
}
}
return max
}