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.

Example 1:

Input: [[0,30],[5,10],[15,20]]
Output: false

Example 2:

Input: [[7,10],[2,4]]
Output: true

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

这道题给了我们一堆会议的时间,问能不能同时参见所有的会议,这实际上就是求区间是否有交集的问题,那么最简单暴力的方法就是每两个区间比较一下,看是否有 overlap,有的话直接返回 false 就行了。比较两个区间a和b是否有 overlap,可以检测两种情况,如果a的起始位置大于等于b的起始位置,且此时a的起始位置小于b的结束位置,则一定有 overlap,另一种情况是a和b互换个位置,如果b的起始位置大于等于a的起始位置,且此时b的起始位置小于a的结束位置,那么一定有 overlap,参见代码如下:

解法一:

class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
for (int i = ; i < intervals.size(); ++i) {
for (int j = i + ; j < intervals.size(); ++j) {
if ((intervals[i][] >= intervals[j][] && intervals[i][] < intervals[j][]) || (intervals[j][] >= intervals[i][] && intervals[j][] < intervals[i][])) return false;
}
}
return true;
}
};

我们可以先给所有区间排个序,用起始时间的先后来排,然后从第二个区间开始,如果开始时间早于前一个区间的结束时间,则说明会议时间有冲突,返回 false,遍历完成后没有冲突,则返回 true,参见代码如下:

解法二:

class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){return a[] < b[];});
for (int i = ; i < intervals.size(); ++i) {
if (intervals[i][] < intervals[i - ][]) {
return false;
}
}
return true;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/252

类似题目:

Merge Intervals

Meeting Rooms II

参考资料:

https://leetcode.com/problems/meeting-rooms/

https://leetcode.com/problems/meeting-rooms/discuss/67782/C%2B%2B-sort

https://leetcode.com/problems/meeting-rooms/discuss/67786/AC-clean-Java-solution

[LeetCode] Meeting Rooms 会议室的更多相关文章

  1. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  2. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  3. LeetCode Meeting Rooms II

    原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...

  4. LeetCode Meeting Rooms

    原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...

  5. [LeetCode] Meeting Rooms I & II

    Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s ...

  6. [leetcode]252. Meeting Rooms会议室有冲突吗

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  7. [LeetCode] 253. Meeting Rooms II 会议室 II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  8. [LeetCode] 253. Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  9. 252. Meeting Rooms 区间会议室

    [抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...

随机推荐

  1. 读书笔记--SQL必知必会15--插入数据

    15.1 数据插入 使用INSERT语句将行插入(或添加)到数据库表.可能需要特定的安全权限. 插入完整的行 插入行的一部分 插入某些查询的结果 15.1.1 插入完整的行 要求指定表名和插入到新行中 ...

  2. 移动开发那些坑之——safari mobile click事件的冒泡bug

    今天在iphone6 plus的safari上测试这么一段代码: <script> $(document).on('click','.callApp', function() { aler ...

  3. GitHub更新自己Fork的项目

    转自:http://www.tuicool.com/articles/MzMJre github上有个功能叫fork,可以将别人的工程复制到自己账号下.这个功能很方便,但其有一个缺点是:当源项目更新后 ...

  4. 怎样把win7系统下的屏幕设置成护眼的非常柔和的豆沙绿色?

    经常面对电脑会导致眼睛过度疲劳,白色对眼睛的刺激是最大的,所以,最好不要用白色做电脑背景色 设置方法如下: 在桌面点右键选"个性化",接着点主窗口底部的"窗口颜色&quo ...

  5. Golang汇编命令解读

    我们可以很容易将一个golang程序转变成汇编语言. 比如我写了一个main.go: package main func g(p int) int { return p+1; } func main( ...

  6. java中异常抛出后代码还会继续执行吗

    今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element){ i ...

  7. 分享api接口验证模块

    一.前言 权限验证在开发中是经常遇到的,通常也是封装好的模块,如果我们是使用者,通常指需要一个标记特性或者配置一下就可以完成,但实际里面还是有许多东西值得我们去探究.有时候我们也会用一些开源的权限验证 ...

  8. C#调用SendMessage 用法

    函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一.    函数原型:LRESUL ...

  9. 深入Collection集合

    List集合 一.ArraryList: 最基本的集合不多做介绍 二.Vector Vector cn=new  Vector(); A:有特有功能 a:添加       public void ad ...

  10. Debug JDK变量显形

    本文面向的朋友 本文主要说明在使用Eclipse Debug JDK时,看不到变量值的解决办法. 如果您看到上面绿色字体表示不敢兴趣,请一定果断back,如果您不爽,请在下面使劲的拍. Debug J ...