[LeetCode] Meeting Rooms

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

Problem Description:

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


The idea is pretty simple: first we sort the intervals in the ascending order of start; then we check for the overlapping of each pair of neighboring intervals. If they do, then return false; after we finish all the checks and have not returned false, just return true.

Sorting takes O(nlogn) time and the overlapping checks take O(n) time, so this idea isO(nlogn) time in total.

The code is as follows.

 1 class Solution {
 2 public:
 3     bool canAttendMeetings(vector<Interval>& intervals) {
 4         sort(intervals.begin(), intervals.end(), compare);
 5         int n = intervals.size();
 6         for (int i = 0; i < n - 1; i++)
 7             if (overlap(intervals[i], intervals[i + 1]))
 8                 return false;
 9         return true;
10     }
11 private:
12     static bool compare(Interval& interval1, Interval& interval2) {
13         return interval1.start < interval2.start;
14     }
15     bool overlap(Interval& interval1, Interval& interval2) {
16         return interval1.end > interval2.start;
17     } 
18 };

 

目录
相关文章
LeetCode 841:钥匙和房间 Keys and Rooms
题目: ​ 有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间。 ​ 在形式上,对于每个房间 i 都有一个钥匙列表 rooms[i],每个钥匙 rooms[i][j] 由 [0,1,...,N-1] 中的一个整数表示,其中 N = rooms.length。
783 0
|
C++
[LeetCode] Best Meeting Point
Problem Description: A group of two or more people wants to meet and minimize the total travel distance.
1023 0
[LeetCode] Meeting Rooms II
Problem Description: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.
1076 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
247 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
166 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
357 2
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
256 3
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
下一篇
oss云网关配置