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的会议时间,让我们判断是否能够参加所有的会议。每一个会议时间都有start 和 end。只要把array 重新排序一下,按照start从小到大。之后遍历每一个会议时间,如果这个会议时间的end 大于 下一个会议时间的start,就判断false。
起初自己写了一个n^n 的sort,通过后发现速度贼慢,只好重新研究其他人的做法。可以利用Arrays.sort 来直接sort我们的intervals, 但是需要结合comparator。之前都有用Arrays.sort, 但是对于这样的object array就没想到,而且也不会用comparator。顺便稍微调查了一下,Arrays.sort 是用两种排序方法, 1- 快速排序, 2-优化的合并排序。 快速排序主要运用于基本类型(int, short...), 合并排序用于对象类型。两种排序都是O(n logn)。对于object的Arrays.sort,需要override一个compare function, a - b就是ascending排序,从小到大; b - a 就是descending排序。
Java Solution:
Runtime beats 75.89%
完成日期:06/24/2017
关键词:Sort
关键点:如何用Arrays.sort 和 Comparator
/**
* 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)
{
// step 1: sort the intervals
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b)
{
return a.start - b.start;
}
}); // step 2: iterate intervals to check each end is <= next start
for(int i=0; i<intervals.length-1; i++)
{
if(i+1 <intervals.length)
{
if(intervals[i].start == intervals[i+1].start)
return false;
if(intervals[i].end > intervals[i+1].start)
return false;
}
} return true;
}
}
参考资料:
* http://www.programcreek.com/2014/07/leetcode-meeting-rooms-java/
* http://blog.csdn.net/lian47810925/article/details/4689323
* http://www.importnew.com/8952.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 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 ...
- [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] 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 ...
- 252. Meeting Rooms 区间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
- [LeetCode] 253. Meeting Rooms II 会议室之二
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 ...
- 252. Meeting Rooms
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
随机推荐
- cxgrid学习
delphi cxgrid控件哪个属性是设置不能编辑? cxgrid控件cxgridDBTable的OptionsData可以选择操作 cxGrid1DBTableView1下选择cxGrid1DBT ...
- 分区工具fdisk,gdisk,parted
在linux中,当我们给系统添加一块新硬盘时,我们是无法使用的,因为他还没有分区和格式化,只有当我们将新硬盘分区并格式化之后,挂载在某个目录下,才能供我们正常使用,接下来我们要学习三种硬盘分区工具,f ...
- SpringMVC第四篇【参数绑定详讲、默认支持参数类型、自定义参数绑定、RequestParam注解】
参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定- 默认支持的参数类型 从上面的用法我们可以发现,我们可以使用req ...
- 深入浅出AQS之独占锁模式
每一个Java工程师应该都或多或少了解过AQS,我自己也是前前后后,反反复复研究了很久,看了忘,忘了再看,每次都有不一样的体会.这次趁着写博客,打算重新拿出来系统的研究下它的源码,总结成文章,便于以后 ...
- 记录maven 整合SSM框架
一.新建maven项目 建好的项目结构如下图: 还需要做以下配置: 勾选上这两项后,就会自动生成 "src/main/java" 和 "src/main/resour ...
- C++Builder中MessageBox的基本用法
C++Builder中MessageBox的基本用法 返回值:IDYES=Application->MessageBox("","",MBYESNO) i ...
- hdu 5937 -- Equation(搜索)
题目链接 problem description Little Ruins is a studious boy, recently he learned addition operation! He ...
- uvalive 3135 Argus
https://vjudge.net/problem/UVALive-3135 题意: 有一个系统有多个指令,每个指令产生一个编号为qnum的时间,每个指令的触发间隔不相同,现在给出若干个指令,现在的 ...
- 当前页面的url未注册 微信支付
原因1:公众号支付授权目录或测试授权目录设置不正确. 原因2:微信SDK"WxPay.JsApiPay.php"文件中GetOpenid方法中$baseUrl的拼接的结果与支付授权 ...
- File FileStream StreamWriter StreamReader文件读写操作方法
string path = "D:\\AccountChecking\\Test.txt"; string content = "abcdefg\r\nhigklmn\r ...