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
1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8 * } 9 */ 10 public class Solution 11 { 12 public boolean canAttendMeetings(Interval[] intervals) 13 { 14 // step 1: sort the intervals 15 Arrays.sort(intervals, new Comparator<Interval>(){ 16 public int compare(Interval a, Interval b) 17 { 18 return a.start - b.start; 19 } 20 }); 21 22 // step 2: iterate intervals to check each end is <= next start 23 for(int i=0; i<intervals.length-1; i++) 24 { 25 if(i+1 <intervals.length) 26 { 27 if(intervals[i].start == intervals[i+1].start) 28 return false; 29 if(intervals[i].end > intervals[i+1].start) 30 return false; 31 } 32 } 33 34 return true; 35 } 36 }
参考资料:
* 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
转载于:https://www.cnblogs.com/jimmycheng/p/7076019.html
LeetCode 252. Meeting Rooms (会议室)$相关推荐
- LeetCode 252. Meeting Rooms
原题链接在这里:https://leetcode.com/problems/meeting-rooms/ 题目: Given an array of meeting time intervals co ...
- 252.Meeting Rooms
/** 252.Meeting Rooms* 2016-6-20 by Mingyang* Arrays.sort的用法要熟悉* 这题和Merge Intervals很像,我们按开始时间把这些Inte ...
- Leetcode 252, 253. Meeting Rooms
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- Meeting Rooms II -- LeetCode
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode 252. 会议室(排序)
文章目录 1. 题目 2. 解题 1. 题目 给定一个会议时间安排的数组,每个会议时间都会包括开始和结束的时间 [[s1,e1],[s2,e2],...] (si < ei),请你判断一个人是否 ...
- LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本
1. Array (1) 27. Remove Element(Easy) 给定数组nums和值val,在适当位置删除该值的所有实例并返回新长度. 思路: 不需要使用排序,如果等于该值,则将n-1的值 ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
最新文章
- [Head First设计模式]身边的设计模式——适配器模式
- 2002: [Hnoi2010]Bounce 弹飞绵羊
- 一个客户端刷新的例子
- 【C++学习】对私有构造析构函数的思考:new一个类对象vs直接创建类对象
- Python中的yield详解
- SparkSession对象
- 上位机软件与西门子PLC
- w ndows7旗舰版怎么重装系统,windows7旗舰版iso怎么安装
- android wifi分析 实现原理,android framework wifi 开启原理
- GD32 汽车诊断协议 J1850-VPW 测试
- 五-2、vue中引用文件路径问题
- 金蝶EAS_WFLL2开发记录
- linux查看文件命令
- inner join 和outer join的区别
- 【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】
- 刷机需要的常识双清,BL,REC,TWRP,ROM
- Rockland Immunochemicals丨GFP抗体-荧光素结合物
- 【转】超级珍贵的卤水秘方大全!
- 模板应用到多个主机 zabbix_玩转zabbix之快速入门,超全组件讲解
- s5pv210的工作模式