LeetCode 252. Meeting Rooms

Question

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

Example 1:

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

Example 2:

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

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti < endi <= 106

Source: https://leetcode.com/problems/meeting-rooms/

Solution

Sort intervals by start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// sort, O(nlog(n))
public boolean canAttendMeetings(int[][] intervals) {
// sort intervals by the start in ascending order
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
for (int i = 1; i < intervals.length; i++) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
return true;
}
Author

Weihao Ye

Posted on

2022-01-24

Updated on

2022-01-24

Licensed under