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. HTML5 文件域+FileReader 分段读取文件并上传到服务器(六)

    说明:使用Ajax方式上传,文件不能过大,最好小于三四百兆,因为过多的连续Ajax请求会使后台崩溃,获取InputStream中数据会为空,尤其在Google浏览器测试过程中. 1.简单分段读取文件为 ...

  2. (转)smarty实现多级分类的方法

    --http://www.aspku.com/kaifa/php/44679.html 这篇文章主要介绍了smarty实现多级分类的方法,涉及循环读取的技巧,非常具有实用价值,需要的朋友可以参考下   ...

  3. PreferenceFragment界面透明问题

    PreferenceFragment界面默认是透明的 而其布局代码框架为 <PreferenceScreen> ... </PreferenceScreen>,背景色及透明度属 ...

  4. appium系列教程(转载)

    1.系列文章:转载来源:乙醇的cnblog http://www.kuqin.com/shuoit/20140704/340994.html 2.环境部署:http://www.51testing.c ...

  5. Gprinter Android SDK V1.0 使用说明

    佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...

  6. NgNice项目案例

    技术构架: 服务端技术:NodeJS + Express4.x + Mongodb + Mongoose 前端技术: AngularJS1.2.x + Bootstrap3.x 源码:https:// ...

  7. 绘图quartz之渐变

    实现线性渐变   径向渐变   自己新建的view中有一个drawRect:(cgrect)rect方法 在这个方法里 可以通过画图 将内容显示在画板上(即最下边的view)   渐变的方式分两种   ...

  8. JS入门笔记

    DOM有四种节点: 1. 元素节点:即标签2. 属性节点:写在标签里的属性3. 文本节点:嵌在元素节点里展示出来的文本4. 文档节点:document 获取元素节点的三种常用方法: 1.ById 2. ...

  9. 掌握JS

    1.原生的js,好比全真教的武功,一步步从基础开始(先练气再御剑),很长一段时间内和jquery有很大差距,掌握以后发现jquery只不过是另外一种武功,看一遍既会.且当学原生到一定程度之后,可以自创 ...

  10. SGU 188.Factory guard

    模拟 code #include <iostream> #include <cstdio> #define LEN 1000 using namespace std; int ...