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

按照start来给intervals排序,然后检查相邻的interval是否有重叠

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        if(intervals == null || intervals.length <= 1)
            return true;

        Arrays.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval i1, Interval i2) {
                return i1.start - i2.start;
            } 
        });

        for(int i = 0; i < intervals.length - 1; i++) {
            Interval i1 = intervals[i];
            Interval i2 = intervals[i + 1];

            if(i2.start < i1.end)
                return false;
        }

        return true;
    }
}

results matching ""

    No results matching ""