-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeetingRoomsIIProblem.java
49 lines (39 loc) · 1.3 KB
/
MeetingRoomsIIProblem.java
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
48
49
package main.com.sumit.coding.companies.google.arraysAndStrings;
import java.util.Arrays;
/*
* Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.
*
* Input: intervals = [[0,30],[5,10],[15,20]]
* Output: 2
* */
public class MeetingRoomsIIProblem {
public static void main(String[] args) {
int[][] intervals = {{0, 30}, {5, 10}, {15, 20}};
System.out.println(minMeetingRooms(intervals));
}
/*
* Time Complexity : O(NlogN)
* Space Complexity : O(N)
* */
public static int minMeetingRooms(int[][] intervals) {
if (intervals.length == 0) return 0;
int rooms = 0, len = intervals.length, startPtr = 0, endPtr = 0;
Integer[] start = new Integer[len];
Integer[] end = new Integer[len];
for (int i = 0; i < len; i++) {
start[i] = intervals[i][0];
end[i] = intervals[i][1];
}
Arrays.sort(start, (a, b) -> a - b);
Arrays.sort(end, (a, b) -> a - b);
while (startPtr < intervals.length) {
if (start[startPtr] >= end[endPtr]) {
rooms--;
endPtr++;
}
rooms++;
startPtr++;
}
return rooms;
}
}