[lintcode] problem 920 – meeting rooms

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.

Example

No.1

Input: intervals = [(0,30),(5,10),(15,20)]

Output: false

Explanation:
(0,30), (5,10) and (0,30),(15,20) will conflict

No.2

Input: intervals = [(5,8),(9,15)]

Output: true

Explanation:
Two times will not conflict

Code

1
2
3
4
5
6
7
8
public class  {
int start, end;

Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public boolean canAttendMeetings(List<Interval> intervals) {
if (intervals == null || intervals.size() < 1)
return true;

Collections.sort(intervals, new Comparator<Interval>() {

public int compare(Interval o1, Interval o2) {
return o1.start - o2.start;
}
});

int end = intervals.get(0).end;

for (int i = 1; i < intervals.size(); i++) {
if (intervals.get(i).start < end)
return false;

end = Math.max(end, intervals.get(i).end);
}

return true;
}