题目:

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. SQL 查询分析器操作(修改、添加、删除)表及字段等

    一.库操作1..创建数据库命令:create database <数据库名>例如:建立一个名为xhkdb的数据库mysql> create database xhkdb; 2.显示所 ...

  2. CLR via C# 混合线程同步构造

    1. 自旋,线程所有权和递归 2. 混合构造 a.ManualResetEventSlim b.SemaphoreSlim c.Monitor d.ReaderWriterLockSlim 3.条件变 ...

  3. python with语句上下文管理的两种实现方法

    在编程中会经常碰到这种情况:有一个特殊的语句块,在执行这个语句块之前需要先执行一些准备动作:当语句块执行完成后,需要继续执行一些收尾动作.例如,文件读写后需要关闭,数据库读写完毕需要关闭连接,资源的加 ...

  4. Golang学习笔记

    一.基础 1. Hello World程序 demo: package main import "fmt" // 注释 //注释 func main() { fmt.Printf( ...

  5. 学C++之感悟

    程序设计真的就这么难得入门啊 最要命的事情就是看那些看不懂的书.断断续续地看C++Primer好几天了,还是一点眉目都没有,稀里糊涂的.看得头疼了用Google找过来人留下的东西看,无意中发现了一篇自 ...

  6. mysql 查询

    查询数据:select s_name from student limit 1;//限制数量 select * from student where s_id in (select s_id from ...

  7. JavaScript Tutorial

    JavaScript Tutorial http://javascript.info/root Object.create rabit.hasOwnProperty('eats') Object.ge ...

  8. iOS 下拉菜单 FFDropDownMenu自定义下拉菜单样式实战-b

    Demo地址:https://github.com/chenfanfang/CollectionsOfExampleFFDropDownMenu框架地址:https://github.com/chen ...

  9. Careercup - Facebook面试题 - 6139456847347712

    2014-05-01 01:50 题目链接 原题: Microsoft Excel numbers cells ... and after that AA, AB.... AAA, AAB...ZZZ ...

  10. 2877: [Noi2012]魔幻棋盘 - BZOJ

    DescriptionInput 第一行为两个正整数N,M,表示棋盘的大小. 第二行为两个正整数X,Y,表示棋盘守护者的位置. 第三行仅有一个正整数T,表示棋盘守护者将进行次操作. 接下来N行,每行有 ...