Skip to content

Commit e4e26ad

Browse files
authored
Merge pull request #207 from rahulshishodia/patch-1
added java solution for Meeting Rooms II
2 parents aa3c57a + fbacfa0 commit e4e26ad

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: java/253-Meeting-Rooms-ii.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition of Interval:
3+
* public class Interval {
4+
* int start, end;
5+
* Interval(int start, int end) {
6+
* this.start = start;
7+
* this.end = end;
8+
* }
9+
* }
10+
*/
11+
12+
public class Solution {
13+
/**
14+
* @param intervals: an array of meeting time intervals
15+
* @return: the minimum number of conference rooms required
16+
*/
17+
public int minMeetingRooms(List<Interval> intervals) {
18+
if(intervals.isEmpty()) return 0;
19+
20+
Collections.sort(intervals, (a, b) -> Integer.compare(a.start, b.start));
21+
22+
Queue<Interval> queue = new PriorityQueue<>((a, b) -> Integer.compare(a.end, b.end));
23+
24+
int count = 0;
25+
for (Interval interval : intervals) {
26+
27+
while(!queue.isEmpty() && interval.start >= queue.peek().end) queue.poll();
28+
29+
queue.offer(interval);
30+
count = Math.max(count, queue.size());
31+
}
32+
return count;
33+
}
34+
}

0 commit comments

Comments
 (0)