252. Meeting Rooms
Posted CodesKiller
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
.
此题重点是理解题意,会议时间不可以存在交集。
其次,了解排序,即数组有数组排序,Arrays.sort. Collections.sort. PriorityQueue()三种排序方式,都可以重写来实现。
代码如下:
/**
* 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的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 252, 253. Meeting Rooms