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] 252. Meeting Rooms 会议室的更多相关文章

  1. LeetCode 252. Meeting Rooms (会议室)$

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

  2. [LeetCode#252] Meeting Rooms

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

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

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

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

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

  5. [LeetCode] Meeting Rooms 会议室

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

  6. 252. Meeting Rooms 区间会议室

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

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

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

  8. 【LeetCode】252. Meeting Rooms 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

  9. 252. Meeting Rooms

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

随机推荐

  1. C#刷遍Leetcode系列连载 索引

    C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...

  2. C# 下载泛型数据

    public class ExportTByNPOI { [STAThread] public static void ExportTDataByNPOI<T>(List<T> ...

  3. c# 调用接口返回json

    需要命名空间 using System.Net; using System.Net.Security using System.Security.Cryptography.X509Certificat ...

  4. python 跟踪IP模块

    #coding=utf-8 import re import subprocess def tracertIP(ip): p = subprocess.Popen(['tracert',ip],std ...

  5. 图书推荐《图解HTTP》

    作品简介 本书对互联网基盘——HTTP协议进行了全面系统的介绍.作者由HTTP协议的发展历史娓娓道来,严谨细致地剖析了HTTP协议的结构,列举诸多常见通信场景及实战案例,最后延伸到Web安全.最新技术 ...

  6. 6 、 图论—NP 搜索

    6.1 最大团 //最大团 //返回最大团大小和一个方案,传入图的大小 n 和邻接阵 mat //mat[i][j]为布尔量 #define MAXN 60 void clique(int n, in ...

  7. Qt程序app添加图标复制到其它电脑后不显示的解决方法

    原因: 主是要因为Qt显示图标需要依赖一些库来进行转换,而复制到其它电脑后不显示,是没有复制相应的库所致,所以把相应库复制过去就行了. 复制Qt的plugins目录下的imageformats文件到程 ...

  8. css中的baseline

    这是css中的一个容易被人忽略的概念,今天在知乎上看到一个问题,这个问题应该是关于baseline,才去补习了一下关于baseline的知识,首先我来还原一下问题: <div style=&qu ...

  9. Linux命令: ps

    STAT 进程状态 S-睡眠 s-进程是会话向导进程 N拥有比普通优先级更低的 R-正在运行 D-短期等待 Z-僵尸进程 T被跟踪或者被停止 STATED 进程启动时间 TIME  进程使用CPU时间 ...

  10. PHP导出3w条数据成表格

    亲测有效,三万条数据秒秒钟导出 先进行数据表插入数据 ini_set('memory_limit','1024M'); //设置程序运行的内存 ini_set('max_execution_time' ...