'lintcode 会议室'

描述

给定一系列的会议时间间隔,包括起始和结束时间[[s1,e1],[s2,e2],…(si < ei),确定一个人是否可以参加所有会议。

样例

样例1

输入: intervals = [(0,30),(5,10),(15,20)]
输出: false
解释:
(0,30), (5,10) 和 (0,30),(15,20) 这两对会议会冲突
样例2

输入: intervals = [(5,8),(9,15)]
输出: true
解释:
这两个时间段不会冲突

思路

根据开始时间和结束时间排序,只要前一个会议的结束时间不晚于后一个会议的开始时间就可以参加

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
* }
*/

class Solution {
public:
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/

static bool cmp(Interval a, Interval b) {

return a.start == b.start ? a.end <= b.end : a.start < b.start;
}

bool canAttendMeetings(vector<Interval> &intervals) {
// Write your code here
if(intervals.size() == 0) return true;
sort(intervals.begin(), intervals.end(), cmp);
for (int i = 0; i < intervals.size() - 1; i++) {
if (intervals[i].end > intervals[i+1].start)
return false;
}
return true;
}
};
-------------end of file thanks for reading-------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值