File tree 1 file changed +40
-0
lines changed
Filter options
1 file changed +40
-0
lines changed
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for an interval.
3
+ * public class Interval {
4
+ * int start;
5
+ * int end;
6
+ * Interval() { start = 0; end = 0; }
7
+ * Interval(int s, int e) { start = s; end = e; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public int minMeetingRooms (Interval [] intervals ) {
12
+ if (intervals .length == 0 ) {
13
+ return 0 ;
14
+ }
15
+
16
+ // building min heap
17
+ PriorityQueue <Integer > allocator = new PriorityQueue <Integer >(intervals .length , new Comparator <Integer >() {
18
+ public int compare (Integer a , Integer b ) {
19
+ return a - b ;
20
+ }
21
+ });
22
+
23
+ // sorting interval by startime
24
+ Arrays .sort (intervals , new Comparator <Interval >() {
25
+ public int compare (Interval a , Interval b ) {
26
+ return a .start - b .start ;
27
+ }
28
+ });
29
+
30
+ allocator .add (intervals [0 ].end );
31
+
32
+ for (int i =1 ; i <intervals .length ; i ++) {
33
+ if (intervals [i ].start >= allocator .peek ()) {
34
+ allocator .poll ();
35
+ }
36
+ allocator .add (intervals [i ].end );
37
+ }
38
+ return allocator .size ();
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments