LeetCode – Meeting Rooms (Java)

Given an array of meeting time intervals consisting of start and end times [s1, e1], [s2, e2], … , determine if a person could attend all meetings.

For example,
Given [ [0, 30], [5, 10], [15, 20] ],
return false.

Java Solution

If a person can attend all meetings, there must not be any overlaps between any meetings. After sorting the intervals, we can compare the current end and next start.

public boolean canAttendMeetings(Interval[] intervals) {
    Arrays.sort(intervals, new Comparator<Interval>(){
        public int compare(Interval a, Interval b){
            return a.start-b.start;
        }
    });
 
    for(int i=0; i<intervals.length-1; i++){
        if(intervals[i].end>intervals[i+1].start){
            return false;
        }
    }
 
    return true;
}

2 thoughts on “LeetCode – Meeting Rooms (Java)”

  1. bucket sort could only be used if the start and end time constraisn were bound to <=1e7 , but in the question the constrains were <=1e9 , so we have to use sorting acc to start time + 2 pointer methods !

  2. i guess people like to sort too much!!!

    use bucket sort 1..24
    mark start time as positive, mark end time as negative

    obv, it must be +- +-…

    if end meeting same as start other meeting , i guess it is overlap

Leave a Comment