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
.
链接: 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的更多相关文章
- [LeetCode] 252. Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 252. Meeting Rooms 区间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
- [LeetCode#252] Meeting Rooms
Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...
- LeetCode 252. Meeting Rooms (会议室)$
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [leetcode]252. Meeting Rooms会议室有冲突吗
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LC] 252. Meeting Rooms
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 【LeetCode】252. Meeting Rooms 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- [LeetCode] 253. Meeting Rooms II 会议室 II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
随机推荐
- CSS练习
看到一个CSS参考手册:http://css.doyoe.com/ 感谢感谢!
- Thinkphp常用的方法和技巧(转)
2012年09月26日 Thinkphp 里一些常用方法和技巧的整理,包括常用的快捷键以及在程序开发时用到的一些实用方法,关于快捷键用得不是很熟练,总之,掌握这些方法和技巧,对于我们开发 thinkp ...
- Windows 7 搭建 nodejs、npm、express 环境
准备工作: 下载nodejs (exe format)(http://nodejs.org/) 下载npm(zip format)(http://nodejs.org/dist/npm/) 开始安装 ...
- Entity Framework Power Tools安装和使用
Entity Framework Power Tools是一个由EntityFramework开发小组提供的工具,它可以从现有数据库生成Fluent款式的Code First代码. 大致来说,这个工具 ...
- 【转】java获取当前路径的几种方法
1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...
- 关于cookie的一点知识
关于cookie的一点知识 1.cookie是存储在客户端计算机中. 2.cookie不能跨浏览器访问.cookie是浏览器保存的,所以不同浏览器对cookie的保存路径.存储数据的格式.文件大小都可 ...
- spring mvc 学习笔记【1】---前言
扎扎实实地掌握好技术 ----------------------------------------------------------------------------------------- ...
- angular 服务
在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了.服务用于在控制器之间进 ...
- Hbase的安装测试工作
Hbase的安装测试工作: 安装:http://www.cnblogs.com/neverwinter/archive/2013/03/28/2985798.html 测试:http://www.cn ...
- [bzoj 2159]Crash的文明世界
今天看到一个鬼题 心情好的时候写 [题意]求树上所有点对距离的k次方和,所有边权为1 大爷方的题解:http://tonyfang.is-programmer.com/posts/204972.htm ...