0%

Leetcode252-meetingRooms

Description

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

Example 1:

1
2
Input: [[0,30],[5,10],[15,20]]
Output: false

Example 2:
1
2
Input: [[7,10],[2,4]]
Output: true

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

Solution

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
class Solution {
class Pair{
int start;
int end;
public Pair(int start, int end){
this.start = start;
this.end = end;
}
}
public boolean canAttendMeetings(int[][] intervals) {
if (intervals == null || intervals.length == 0 || intervals[0].length == 0) return true;
Pair[] inter = new Pair[intervals.length];
for (int i = 0; i < intervals.length; i++){
inter[i] = new Pair(intervals[i][0], intervals[i][1]);
}
Arrays.sort(inter, new Comparator<Pair>(){
@Override
public int compare(Pair a, Pair b){
return a.start - b.start;
}
});
for (int i = 0; i < inter.length - 1; i++){
if (inter[i].end > inter[i + 1].start)
return false;
}
return true;
}
}