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

代码如下:

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

/**

* 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;

}

}

时间: 2024-10-25 07:54:02

252. Meeting Rooms的相关文章

LeetCode 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. 题目标签:sort 这道题目给了我们一个array的会议时间,让我们

[LeetCode#252] Meeting Rooms

Problem: 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. Analysis: The problem is

[LC] 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. Example 1: Input: [[0,30],[5,10],[15,20]] Output: false Example 2: Input: [[7,10],[2,4]] Outp

[LeetCode] Meeting Rooms I &amp; II

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. 1 /** 2 * Definition

[LeetCode] Meeting Rooms II 会议室之二

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. 这道题是之前那道Meeting Rooms的拓展,那道题只让我们是

Leetcode 252, 253. 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. 所有meeting time按照start排序之后,逐个检查当前的s

253 Meeting Rooms II

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example, Given [[0, 30],[5, 10],[15, 20]], return 2. 看到区间求重叠的部分 , 就想到对区间排序(start, en

[LeetCode#253] Meeting Rooms II

Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. Analysis: This problem l

Meeting Rooms II -- LeetCode

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. 思路:贪心. 将所有interval按照开始时间从早到晚排序.之后