252. Meeting Rooms (1)
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 1:
Input: [[0,30],[5,10],[15,20]]
Output: false
Example 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
TimeComplexity: O(nlogn), by sort
class Solution {
public boolean canAttend(int[][] intervals) {
//need to sort before checking, otherwise will misjudge the ovelap case
//ex: {[1,4][0,0]} -> misjude the [0,0] is overlapped with [1,4]
Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
if(intervals.length <= 1){
return true;
}
int preStart = intervals[0][0];
int preEnd = intervals[0][1];
for(int i = 1; i < intervals.length; i++){
int curStart = intervals[i][0];
int curEnd = intervals[i][1];
//has overlap
if(curStart < preEnd ){
return false
}
}
return true;
}
}
Last updated
Was this helpful?