MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / minMeetingRooms

Method minMeetingRooms

java/0253-meeting-rooms-ii.java:18–40  ·  view source on GitHub ↗

@param intervals: an array of meeting time intervals @return: the minimum number of conference rooms required

(List<Interval> intervals)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

isEmptyMethod · 0.45
compareMethod · 0.45
peekMethod · 0.45
sizeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected