Meeting Room

Problem

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.

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

Solution

Sorting Solution

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

Analysis

Just sort the given intervals by either start or end
Then loop through the entire intervals to see if there exists two has conflict time
intervals[i + 1].start < intervals[i].end or intervals[i].end > intervals[i + 1].start

results matching ""

    No results matching ""