首页 > 代码库 > 252. Meeting Rooms

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.

此题重点是理解题意,会议时间不可以存在交集。

其次,了解排序,即数组有数组排序,Arrays.sort. Collections.sort. PriorityQueue()三种排序方式,都可以重写来实现。

代码如下:

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 }</style>

/**

 * 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.length==0) return true;

        PriorityQueue<Interval> q = new PriorityQueue<Interval>(intervals.length,new Comparator<Interval>(){

            public int compare(Interval a,Interval b){

                return a.start-b.start;

            }

        });

        for(Interval i:intervals){

            q.offer(i);

        }

        while(!q.isEmpty()){

            Interval pre = q.poll();

            if(!q.isEmpty()&&pre.end>q.peek().start) return false;

        }

        return true;

    }

}

252. Meeting Rooms