题目:

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.

链接: http://leetcode.com/problems/meeting-rooms/

题解:

一开始以为是跟Course Schedule一样,仔细读完题目以后发现只要sort一下就可以了。写得还不够精简,需要好好研究一下Java8的lambda表达式。

Time Complexity - O(nlogn), Space Complexity - O(1)

/**
* 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)
return true;
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval t1, Interval t2) {
if(t1.start != t2.start)
return t1.start - t2.start;
else
return t1.end - t2.end;
}
}); for(int i = 1; i < intervals.length; i++) {
if(intervals[i].start < intervals[i - 1].end)
return false;
} return true;
}
}

二刷:

也是先对interval数组进行先startdata再enddate的排序,之后一次遍历数组来看是否intervals[i].start < intervals[i - 1].end。

用lambda表达式以后发现好慢

Java:

Time Complexity - O(nlogn), Space Complexity - O(1)

/**
* 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) {
return true;
}
Arrays.sort(intervals, (Interval i1, Interval i2) -> i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end);
for (int i = 1; i < intervals.length; i++) {
if (intervals[i].start < intervals[i - 1].end) {
return false;
}
}
return true;
}
}

三刷:

Java:

/**
* 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) return true;
Arrays.sort(intervals, (Interval i1, Interval i2) -> i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end);
for (int i = 1; i < intervals.length; i++) {
if (intervals[i].start < intervals[i - 1].end) return false;
}
return true;
}
}

Reference:

https://leetcode.com/discuss/50912/ac-clean-java-solution

252. Meeting Rooms的更多相关文章

  1. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  2. 252. Meeting Rooms 区间会议室

    [抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...

  3. [LeetCode#252] Meeting Rooms

    Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...

  4. LeetCode 252. Meeting Rooms (会议室)$

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. [leetcode]252. Meeting Rooms会议室有冲突吗

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  6. [LC] 252. Meeting Rooms

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  7. 【LeetCode】252. Meeting Rooms 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

  8. [LeetCode] 253. Meeting Rooms II 会议室 II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  9. [LeetCode] Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

随机推荐

  1. Nginx+uWSGI+Django原理

    Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来 ...

  2. COUNT(*),count(1),COUNT(ALL expression),COUNT(DISTINCT expression)

    创建一个测试表 IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; GO )); GO INSERT INT ...

  3. Eclipse C/C++开发环境搭建

    1 Eclipse的安装 到http://java.sun.com/j2se/1.5.0/download.jsp 下载JRE安装: 到http://eclipse.org下载Eclipse安装.(这 ...

  4. .NET序员的成长之路

  5. 序列化form表单内容为json对象

    SourceCode: ; (function ($) { $.fn.extend({ serializeJson: function () { var json = {}; $(this.seria ...

  6. 浅析JVM内存结构和6大区域(转)

    其实对于我们一般理解的计算机内存,它算是CPU与计算机打交道最频繁的区域,所有数据都是先经过硬盘至内存,然后由CPU再从内存中获取数据进行处理,又将数据保存到内存,通过分页或分片技术将内存中的数据再f ...

  7. ORACLE EXPDP命令使用详细【转】

    本文转自:http://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细 相关参数以及导出示例: 1. DIRECTORY ...

  8. c#中获取服务器IP,客户端IP以及其它

    客户端ip:Request.ServerVariables.Get("Remote_Addr").ToString();客户端主机名:Request.ServerVariables ...

  9. _beginthreadex创建多线程详解

    一.需要的头文件支持 #include <process.h>         // for _beginthread() 需要的设置:ProjectSetting-->C/C++- ...

  10. Codeforces 402A 402B 402C 402D

    402A 直接暴力 #include <cstdio> #include <cstdlib> #include <cmath> #include <map&g ...