跳转至

252. Meeting Rooms

Leetcode Sort

Given an array of meeting time intervals consisting of start and end times [[s_1,e_1],[s_2,e_2],...], (s_i < e_i), determine if a person could attend all meetings.

Example

Given intervals = [[0,30],[5,10],[15,20]], return false.

分析

这道题目在LeetCode上要收费,参见LintCode

确认可以参加所有会议,即会议时间不重叠。将会议按照会议开始时间排序,如果前面的会议结束时间小于后面的会议开始时间,则不会产生重叠。

/**
 * Definition of Interval:
 * public class Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 * }
 */
public boolean canAttendMeetings(List<Interval> intervals) {
    Collections.sort(intervals, Comparator.comparing(o -> o.start));
    for (int i = 1; i < intervals.size(); i++)
        if (intervals.get(i).start < intervals.get(i - 1).end) return false;
    return true;
}