-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeetingRooms2.java
More file actions
47 lines (38 loc) · 1.37 KB
/
MeetingRooms2.java
File metadata and controls
47 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],...] (start_i < end_i), find the minimum number of days required to schedule all meetings without any conflicts.
Example 1:
Input: intervals = [(0,40),(5,10),(15,20)]
Output: 2
Explanation:
day1: (0,40)
day2: (5,10),(15,20)
*/
/**
* Definition of Interval:
* public class Interval {
* public int start, end;
* public Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
class Solution {
public int minMeetingRooms(List<Interval> intervals) {
if(intervals == null || intervals.size() == 0)return 0;
//sorting of intervals
intervals.sort(Comparator.comparingInt(i -> i.start));
//minHeap to track the earliest end time
PriorityQueue<Integer> heap = new PriorityQueue<>();
//addd the end time of the first meeting
heap.add(intervals.get(0).end);
for(int i = 1;i < intervals.size();i++){
//if the current meeting starts after the earliest ended one
if (intervals.get(i).start >= heap.peek())
heap.poll(); //our day is free
heap.add(intervals.get(i).end); // add end time of current meeintg
}
// heap size tells the number of days required
return heap.size();
}
}