forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeetingRooms.java
More file actions
43 lines (34 loc) · 1.3 KB
/
MeetingRooms.java
File metadata and controls
43 lines (34 loc) · 1.3 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
package array;
import java.util.Arrays;
/**
* Created by gouthamvidyapradhan on 27/11/2017.
* Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.
Solution: Sort the interval based on the start interval. Then, for every interval check if the previous interval
ends before the start of the current interval.
*/
public class MeetingRooms {
public static class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
}
public static void main(String[] args) throws Exception{
Interval i1 = new Interval(0, 30);
Interval i2 = new Interval(5, 10);
Interval i3 = new Interval(15, 20);
Interval[] intervals = {i1, i2, i3};
System.out.println(new MeetingRooms().canAttendMeetings(intervals));
}
public boolean canAttendMeetings(Interval[] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a.start, b.start));
for(int i = 1; i < intervals.length; i ++){
if(intervals[i].start < intervals[i - 1].end)
return false;
}
return true;
}
}