252. Meeting Rooms

Description

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

Sort, time O(n * log n), space O(1)

比较排序后的intervals是否有重叠即可。

/**
 * 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) {
            return false;
        }
        
        Arrays.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b) {
                return a.start != b.start ? a.start - b.start
                    : a.end - b.end;
            }
        });
        
        for (int i = 1; i < intervals.length; ++i) {
            if (intervals[i].start < intervals[i - 1].end) {
                return false;
            }
        }
        
        return true;
    }
}

你可能感兴趣的:(252. Meeting Rooms)