252 Meeting Rooms

Posted apanda009

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了252 Meeting Rooms相关的知识,希望对你有一定的参考价值。

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.

Implement a Comparator<Interval>

Syntax: don‘t forget the public sign when defining a function

/**
 * 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; }
 * }
 */
public class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        if (intervals==null || intervals.length==0 || intervals.length==1) return true;
        Comparator<Interval> comp = new Comparator<Interval>() {
            public int compare(Interval i1, Interval i2) {
                return (i1.start==i2.start)? i1.end-i2.end : i1.start-i2.start;
            }
        };
        
        Arrays.sort(intervals, comp);
        Interval pre = intervals[0];
        for (int i=1; i<intervals.length; i++) {
            Interval cur = intervals[i];
            if (cur.start < pre.end) return false;
            pre = cur;
        }
        return true;
    }
}

  

public static <T> void sort(T[] a,
                            Comparator<? super T> c)
根据指定比较器产生的顺序对指定对象数组进行排序。

以上是关于252 Meeting Rooms的主要内容,如果未能解决你的问题,请参考以下文章

252 Meeting Rooms

[LC] 252. Meeting Rooms

252.Meeting Rooms

Leetcode 252, 253. Meeting Rooms

LeetCode 252. Meeting Rooms (会议室)

LeetCode Meeting Rooms