[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.
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
类似题目:
参考资料:
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 会议室的更多相关文章
- 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],...] ...
随机推荐
- 这个meta标签会让华为mate10 pro自带浏览器无法粘贴手机收到的验证码信息
前言 最近在项目中遇到一个问题,注册登录界面点击获取验证码,手机收到短信验证码后可以复制成功,但无法粘贴 让人郁闷的是在其它上手机上的(比如小米,苹果)默认浏览器和其它手机浏览器(比如QQ,夸克,搜 ...
- 奥展项目笔记04--Spring cloud 通过父工程打包多个子工程,导出可运行的Jar包
在spring cloud微服务搭建过程中,我们创建了多个微服务模块,如图: 1.父工程Pom文件 <?xml version="1.0" encoding="UT ...
- 【UOJ#48】【UR #3】核聚变反应强度(质因数分解)
[UOJ#48][UR #3]核聚变反应强度(质因数分解) 题面 UOJ 题解 答案一定是\(gcd\)除掉\(gcd\)的最小质因子. 而\(gcd\)的最小值因子一定是\(a_1\)的质因子. 所 ...
- AI与数学笔记之深入浅出的讲解傅里叶变换(真正的通俗易懂)
原文出处: 韩昊 # 作 者:韩 昊 # 知 乎:Heinrich # 微 博:@花生油工人 # 知乎专栏:与时间无关的故事 # 谨以此文献给大连海事大学的吴楠老师,柳晓鸣老师,王新年老师以及张 ...
- SetApartmentState(ApartmentState state).Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process
System.Threading.ThreadStateException: 'Current thread must be set to single thread apartment (STA) ...
- web前端-bootstrap
1.什么是bootstrap 前端开发开源工具包 ,包含css样式库+jq插件 ,ui效果非常好 ,都是通过给标签加class选择器来实现功能的 2.特点 响应式布局:使用栅格系统可以把页面呈现在不同 ...
- React组件安装使用和生命周期函数
React安装在使用react时 需要安装 两个模块 react react-dom 初始化时 需要用到react-dom中的render方法 具体如下: import ReactDOM from & ...
- 最新版windows安装支持输入shell命令的工具cygwin教程
首先去官网下载自己对应系统32位或64位系统版本安装包:https://cygwin.com/install.html 下载好后按提示一步一步安装,直到这一步: 初次安装,这里是空的,没有下载的镜像链 ...
- chrome安装react-devtools开发工具
我开始安装react-devtools的时候 百度了一波,都是写的不清不楚,官网又都是英文的 也不是完全理解,经过一番折腾出来以后,写个文档记录一下,也可避免新手首次安装走弯路 我安装react-de ...
- PHP 简单面向对象 验证码类(静态实例对象调用)
没事写了一个简单的面向对象验证码类,可以直接使用(替换一下字体路径) <?php class authCode { private static $instance = null; #实例对象 ...