Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 64d702b

Browse filesBrowse files
committed
meeting rooms required for meetings
1 parent c19b86c commit 64d702b
Copy full SHA for 64d702b

File tree

1 file changed

+40
-0
lines changed
Filter options

1 file changed

+40
-0
lines changed

‎greedy/meetingRooms2.java

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.