@param intervals: an array of meeting time intervals @return: the minimum number of conference rooms required
(List<Interval> intervals)
| 16 | * @return: the minimum number of conference rooms required |
| 17 | */ |
| 18 | public int minMeetingRooms(List<Interval> intervals) { |
| 19 | if (intervals.isEmpty()) return 0; |
| 20 | |
| 21 | Collections.sort( |
| 22 | intervals, |
| 23 | (a, b) -> Integer.compare(a.start, b.start) |
| 24 | ); |
| 25 | |
| 26 | Queue<Interval> queue = new PriorityQueue<>((a, b) -> |
| 27 | Integer.compare(a.end, b.end) |
| 28 | ); |
| 29 | |
| 30 | int count = 0; |
| 31 | for (Interval interval : intervals) { |
| 32 | while ( |
| 33 | !queue.isEmpty() && interval.start >= queue.peek().end |
| 34 | ) queue.poll(); |
| 35 | |
| 36 | queue.offer(interval); |
| 37 | count = Math.max(count, queue.size()); |
| 38 | } |
| 39 | return count; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Two pointer approach |