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()三种排序方式,都可以重写来实现。

代码如下:

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public boolean canAttendMeetings(Interval[] intervals) {
12         if(intervals==null||intervals.length==0) return true;
13         Comparator<Interval> comp = new Comparator<Interval>(){
14             public int compare(Interval i1,Interval i2){
15                 return i1.start-i2.start;
16             }
17         };
18         Arrays.sort(intervals,comp);
19         //int start  = intervals[0].start;
20         int end = intervals[0].end;
21         for(int i=1;i<intervals.length;i++){
22             if(intervals[i].start-end<0) return false;
23             else{
24                 end = Math.max(end,intervals[i].end);
25             }
26         }
27         return true;
28     }
29 }
30 //the run time could include the time sorting and the compare, so the total run time could be O(nlongn), the sace complexity could be O(1);

 

posted @ 2017-02-03 12:58  CodesKiller  阅读(170)  评论(0编辑  收藏  举报