Skip to content

Commit 729e90f

Browse files
committed
Add meeting rooms2 solution
1 parent ff46bfb commit 729e90f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package java1.algorithms.interval;
2+
import java.util.*;
3+
4+
class Interval {
5+
int start, end;
6+
Interval(int start, int end) {
7+
this.start = start;
8+
this.end = end;
9+
}
10+
}
11+
public class meetingRooms2 {
12+
//Two pointer TC: O(n logn + n) SC: O(n)
13+
private static int minMeetingRooms1(List<Interval> intervals) {
14+
int length = intervals.size();
15+
int[] startTimes = new int[length];
16+
int[] endTimes = new int[length];
17+
int minMeetingRooms = 0, count = 0;
18+
19+
for(int i=0; i<intervals.size(); i++) {
20+
startTimes[i] = intervals.get(i).start;
21+
endTimes[i] = intervals.get(i).end;
22+
}
23+
Arrays.sort(startTimes);
24+
Arrays.sort(endTimes);
25+
26+
int startIndex =0, endIndex = 0;
27+
28+
while(startIndex < intervals.size()) {
29+
if(startTimes[startIndex] < endTimes[endIndex]) {
30+
count++;
31+
startIndex++;
32+
} else {
33+
count--;
34+
endIndex++;
35+
}
36+
minMeetingRooms = Math.max(minMeetingRooms, count);
37+
}
38+
return minMeetingRooms;
39+
}
40+
41+
//Priority Queue + sorting:- TC: O(n logn + n) SC: O(n)
42+
private static int minMeetingRooms2(List<Interval> intervals) {
43+
Collections.sort(intervals, (a, b) -> a.start - b.start);
44+
PriorityQueue<Interval> priorityQueue = new PriorityQueue<>((a, b) -> a.end-b.end);
45+
priorityQueue.add(intervals.get(0));
46+
47+
for(int i=1; i< intervals.size(); i++) {
48+
int endTime = priorityQueue.peek().end;
49+
int startTime = intervals.get(i).start;
50+
if(endTime <= startTime) {
51+
priorityQueue.poll();
52+
}
53+
priorityQueue.add(intervals.get(i));
54+
}
55+
56+
return priorityQueue.size();
57+
}
58+
59+
public static void main(String[] args) {
60+
List<Interval> intervals = new ArrayList<>();
61+
intervals.add(new Interval(2, 7));
62+
intervals.add(new Interval(3, 5));
63+
intervals.add(new Interval(3, 9));
64+
intervals.add(new Interval(5, 12));
65+
intervals.add(new Interval(8, 15));
66+
intervals.add(new Interval(9, 14));
67+
System.out.println(minMeetingRooms1(intervals));
68+
System.out.println(minMeetingRooms2(intervals));
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Two pointer TC: O(n logn + n) SC: O(n)
2+
3+
function minMeetingRooms1(intervals) {
4+
let startTimes = Array(intervals.length);
5+
let endTimes = Array(intervals.length);
6+
7+
for(let [start, end] of intervals) {
8+
startTimes.push(start);
9+
endTimes.push(end);
10+
}
11+
12+
startTimes.sort((a, b) => a-b);
13+
endTimes.sort((a, b) => a-b);
14+
15+
let startTimeIndex = endTimeIndex = 0;
16+
let minMeetingRooms = count = 0;
17+
18+
while(startTimeIndex < intervals.length) {
19+
let startTime = startTimes[startTimeIndex];
20+
let endTime = endTimes[endTimeIndex];
21+
if(startTime < endTime) {
22+
startTimeIndex++;
23+
count++;
24+
} else {
25+
endTimeIndex++;
26+
count--;
27+
}
28+
minMeetingRooms = Math.max(minMeetingRooms, count);
29+
}
30+
return minMeetingRooms;
31+
}
32+
33+
let intervals = [[2, 7], [3, 5], [3, 9], [5, 12], [8, 15], [9, 14]];
34+
console.log(minMeetingRooms1(intervals));

0 commit comments

Comments
 (0)