Meeting Room

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.

分析:

  即判断这些区间是否有重叠,遍历一遍,前一个区间的右边界不大于后一个区间的左边界即可,时间复杂度O(n),空间复杂度O(1)

代码:

bool canAttendAll(vector<vector<int> > time) {
for(int i = ; i < time.size(); i++)
if(time[i][] < time[i - ][])
return false;
return true;
}

Meeting Room II

Given an array of meeting time intervals consisting of start and end times [[s1,e1], [s2,e2],...] (si < ei), find the minimum number of conference rooms required.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.

分析:

  典型贪心法问题,尝试3种方案:1、开始时间优先,可行;2、结束时间优先,可以找到反例,[2,5][4,6][6,10][5,12],2个房间即可,可用该解法得要3个房间,故否决;3、持续时间优先,可以找到反例,[4,5][4,6][6,10][5,12],2个房间即可,可用该解法需要3个房间,故否决;

解法: 

  开始时间优先

证明:

  有[x1, y1],[x2, y2],x1 < x2,y1 > x2,得开两个房间;对于[x3, y3],[x4, y4],必有x4 >= x3 >= x2。那么,若x3 >= y1,将[x3, y3]归入[x1, y1]房间中,则如果x4 < y2,那么x3 < y2,若交换[x3, y3]和[x4, y4]的顺序,还是必然还得多开一个房间,结果无差别;若x4 >= y2,可以将 [x4, y4]归入[x2, y2]房间中,交换[x3, y3]和[x4, y4]的顺序,结果并不会更好,反而可能更差。故开始时间优先的方法是最优的。

代码:

bool cmp(vector<int> &a, vector<int> &b) {
return a[] < b[];
}
int roomCount(vector<vector<int> > time) {
if(time.empty())
return ;
sort(time.begin(), time.end(), cmp);
vector<int> room(, INT_MIN);
int count = ;
for(auto t : time) {
bool openNew = true;
for(int &r : room) {
if(r <= t[]) {
r = t[];
openNew = false;
break;
}
}
if(openNew) {
count++;
room.push_back(t[]);
}
}
return count;
}

  

[Locked] Meeting Room I && II的更多相关文章

  1. meeting room I & II

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

  2. [LeetCode] Meeting Rooms I & II

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

  3. [Locked] Paint House I & II

    Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...

  4. [Locked] Flip Game I & II

    Flip Game I You are playing the following Flip Game with your friend: Given a string that contains o ...

  5. [Locked] Palindrome Permutation I & II

    Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...

  6. 边工作边刷题:70天一遍leetcode: day 84-3

    Meeting Rooms I/II 要点:这题和skyline类似,利用了interval start有序的特点,从左向右处理,用一个heap来动态表示当前占用rooms的时间段,所以heap的si ...

  7. sql语句优化总结

    sql语句优化总结 数据库优化的几个原则: 1.尽量避免在列上做运算,这样会导致索引失败: 2.使用join是应该用小结果集驱动大结果集,同时把复杂的join查询拆分成多个query.不然join的越 ...

  8. BugPhobia开发篇章:Beta阶段第II次Scrum Meeting

    0x01 :Scrum Meeting基本摘要 Beta阶段第二次Scrum Meeting 敏捷开发起始时间 2015/12/13 00:00 A.M. 敏捷开发终止时间 2015/12/14 22 ...

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

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

随机推荐

  1. ^(bitwise exclusive Or).

    一个数,进行异或同一个数两次,将得到原来的数,例如: 6 ^ 4 ^ 4 = 6; 0000-0000-0000-0110 ^ 0000-0000-0000-0100 ---------------- ...

  2. Zend Server安装后首次运行就出现Internal Server Error的解决

    无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...

  3. SGU 159.Self-Replicating Numbers

    时间限制:0.5s 空间限制:6M 题意:         在b(2<b<36)进制中,找到所有长度为n(0<n<2000)的自守数k,满足k^2%b^n=k,字典序输出.   ...

  4. 【POJ2155】【二维树状数组】Matrix

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  5. 【NOI2004】郁闷的出纳员

    [问题描述] OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的 ...

  6. Java 8 与 .Net的平台发展

    从早期版本中,我们已经可以领略到一些令人兴奋的特性.但是开发真Andrew C.Oliver表示,尽管如此,Java语言在某些特性上还是落后于.Net.比如,Java 8 中最令人期待的Lambda表 ...

  7. SQL 左外连接查询 将右表中的多行变为左表的一列或多列

    示例: --行列互转 /**************************************************************************************** ...

  8. jQuery 元素移除empty() remove()与detach()的区别?

    @1.empty() 删除匹配元素集合中所有的后代字节点元素: <p>hello<span>world</span></p> $("p&quo ...

  9. jquery 判断页面滚动到底部

    $(document).scrollTop() 获取垂直滚动的距离 即当前滚动的地方的窗口顶端到整个页面顶端的距离$(document).scrollLeft() 这是获取水平滚动条的距离获取顶端 只 ...

  10. 使用js判断一个对象是否为空 比如 obj={}

    今天使用到js判断一个对象是否为空,js没有封装好的方法,这里最好的办法就是使用jquery里面的封装好的方法 $.isEmptyObject(obj)