1
+ /**
2
+ * Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei),
3
+ * find the minimum number of conference rooms required.
4
+
5
+ Example 1:
6
+
7
+ Input: [[0, 30],[5, 10],[15, 20]]
8
+ Output: 2
9
+ Example 2:
10
+
11
+ Input: [[7,10],[2,4]]
12
+ Output: 1
13
+ */
14
+ class MinHeap {
15
+ constructor ( ) {
16
+ this . heap = [ null ] ;
17
+ }
18
+
19
+ insert ( value ) {
20
+ this . heap . push ( value ) ;
21
+ if ( this . heap . length > 2 ) {
22
+ let idx = this . heap . length - 1 ; // Get the index of the last element of heap
23
+
24
+ while ( this . heap [ idx ] < this . heap [ Math . floor ( idx / 2 ) ] ) {
25
+ if ( idx >= 1 ) {
26
+ [ this . heap [ Math . floor ( idx / 2 ) ] , this . heap [ idx ] ] = [ this . heap [ idx ] , this . heap [ Math . floor ( idx / 2 ) ] ] ;
27
+
28
+ if ( Math . floor ( idx / 2 ) > 1 ) {
29
+ idx = Math . floor ( idx / 2 ) ;
30
+ } else {
31
+ break ;
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ remove ( ) {
39
+ let smallest = this . heap [ 1 ] ;
40
+
41
+ if ( this . heap . length > 2 ) {
42
+ this . heap [ 1 ] = this . heap [ this . heap . length - 1 ] ;
43
+ this . heap . splice ( this . heap . length - 1 ) ;
44
+
45
+ if ( this . heap . length === 3 ) {
46
+ if ( this . heap [ 1 ] > this . heap [ 2 ] ) {
47
+ [ this . heap [ 1 ] , this . heap [ 2 ] ] = [ this . heap [ 2 ] , this . heap [ 1 ] ] ;
48
+ }
49
+ return smallest ;
50
+ }
51
+
52
+ let i = 1 ;
53
+ let left = 2 * i ;
54
+ let right = ( 2 * i ) + 1 ;
55
+
56
+ while ( this . heap [ i ] >= this . heap [ left ] || this . heap [ i ] >= this . heap [ right ] ) {
57
+ if ( this . heap [ left ] < this . heap [ right ] || ! this . heap [ right ] ) {
58
+ [ this . heap [ i ] , this . heap [ left ] ] = [ this . heap [ left ] , this . heap [ i ] ] ;
59
+ i = 2 * i ;
60
+ } else {
61
+ [ this . heap [ i ] , this . heap [ right ] ] = [ this . heap [ right ] , this . heap [ i ] ] ;
62
+ i = ( 2 * i ) + 1 ;
63
+ }
64
+ left = 2 * i ;
65
+ right = ( 2 * i ) + 1 ;
66
+ if ( this . heap [ left ] == undefined && this . heap [ right ] == undefined ) {
67
+ break ;
68
+ }
69
+ }
70
+ } else if ( this . heap . length === 2 ) {
71
+ this . heap . splice ( 1 , 1 ) ;
72
+ } else {
73
+ return null ;
74
+ }
75
+
76
+ return smallest ;
77
+ }
78
+
79
+ sort ( ) {
80
+ const result = [ ] ;
81
+
82
+ while ( this . heap . length > 1 ) {
83
+ result . push ( this . remove ( ) ) ;
84
+ }
85
+
86
+ return result ;
87
+ }
88
+
89
+ size ( ) {
90
+ return this . heap . length ;
91
+ }
92
+
93
+ peek ( ) {
94
+ return this . heap [ 1 ] ;
95
+ }
96
+ }
97
+
98
+ const minMeetingRooms = intervals => {
99
+ intervals = intervals . sort ( ( [ a , b ] , [ c , d ] ) => a - c ) ;
100
+
101
+ let minHeap = new MinHeap ( ) ;
102
+
103
+ minHeap . insert ( intervals [ 0 ] [ 1 ] ) ; // adding end time to minHeap
104
+
105
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
106
+ if ( intervals [ i ] [ 0 ] >= minHeap . peek ( ) ) {
107
+ minHeap . remove ( ) ;
108
+ }
109
+
110
+ // we need to add the end time of interval in both cases
111
+ // 1. when we add a new meeting
112
+ // 2. or update an existing meeting with new end time
113
+ minHeap . insert ( intervals [ i ] [ 1 ] ) ;
114
+ }
115
+
116
+ return minHeap . size ( ) - 1 ;
117
+ }
0 commit comments