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],...] ...
随机推荐
-
LINQ to DataSet的DataTable操作
1. DataTable读取列表 DataSet ds = new DataSet();// 省略ds的Fill代码DataTable products = ds.Tables["Produ ...
-
MVVM模式应用 之xml文件的读取
XML如下所示: <?xml version="1.0" encoding="utf-8" ?> <schools> <schoo ...
-
内核调试打印dump_stack
https://blog.csdn.net/dragon101788/article/details/9419175 在函数中加入dump_stack函数 需要包含的头文件: #include < ...
-
cmd非运行完再保存,结果显示&;保存同时进行
#coding:utf-8 """ fps信息获取到文件 """ import sys import subprocess class Lo ...
-
roadhog 介绍
官方网站:https://www.npmjs.com/package/roadhog; 项目搭建demo: https://github.com/ght5935/antd-dva-less-webpa ...
-
FAT32文件系统学习(上)
2011-06-02 22:30:48 目的:需要编写SD读图片的底层驱动程序.所以要了解一个SD卡常用文件系统基本概念.累计学习用时2.5小时. 一,FAT32的保留区 1,引导扇区 :引导扇区是F ...
-
MonologFX最简demo,javafx外用dialog示例
参考blog:https://blogs.oracle.com/javajungle/entry/monologfx_floss_javafx_dialogs_for /* * To change t ...
-
C. Sad powers
You're given Q queries of the form (L, R). For each query you have to find the number of such x that ...
-
Cognos 报表在列表上面显示汇总
一直以来,Cognos Report Studio设计报表的时候,汇总默认显示在列表下方: 1如图,拖一个列表 2运行如下,数据显示正常按日期排序 3选中订单笔数.订单金额,添加自动汇总 4:运行,可 ...
- javascript 对象 原型 prototype