题目:

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. .NET开发Windows Service程序 - Topshelf

    在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...

  2. COALESCE在SQL拼接中的大用途

    SQL拼接可以使得代码比较灵活,不会那么死板,对于维护也比较方便. 下面是简单的SQL拼接,同时也包含了隐式游标的概念吧,可以遍历表中的每一个字段 -------------------------- ...

  3. [转]一个基于完成端口的TCP Server Framework,浅析IOCP

    [转]一个基于完成端口的TCP Server Framework,浅析IOCP http://www.cppblog.com/adapterofcoms/archive/2010/06/26/1187 ...

  4. Java中如何防止内存泄漏的发生

    在Java开发中我们常常会遇到内存泄漏的情况发生.那么为什么会发生内存泄漏,以及怎样去防止! 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 为什么会发生 ...

  5. C/C++ 内联函数

    内联函数具备一般函数的性质,但是不需要调用,而是在编译阶段,会用函数体替换函数名被调用的地方.可以节省调用时间(进出栈.保存上下文). 在编译层面和宏的作用相同.内联函数的展开在编译阶段,宏展开在预处 ...

  6. 【原创】jQuery插件 - Booklet翻书特效教程(一) 一般设置

    jQuery插件 - Booklet翻书特效教程(一) 一般设置 本文由五月雨恋提供,转载请注明出处. 一.宽高(width/height) 1.自定义大小 $(function(){ // 自定义页 ...

  7. C# 获取数组的子集

    private static void PrintSubItems(int[] source) { int i = 1; int total = int.Parse(Math.Pow(2, sourc ...

  8. iOS中 @synthesize 和 @dynamic

    今天写点过时的东西,我记得  这个是xcode 4 那个年代的事情了,没想到有时候大家还会被问到.可能目的就是看看你是从是么时候才开始接触iOS的. 在声明property属性后,有2种实现选择 @s ...

  9. git撤销删除

    问题描述:     使用git时本地文件删除了,提交至github,希望撤销修改,找回源文件 问题解决: (1)查看git log,查看日志信息 注:     使用 git log 可以查看提交的日志 ...

  10. JS Web打印,实现预览新样式

    问题描述:     JS实现Web打印,要求打印前一种样式,打印预览时新样式 问题解决:         (1)设置打印时的css样式,设置打印前的css样式 注:         以上为print. ...