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. ViewPager + Fragment 实现类微信界面

    在如今的互联网时代,微信已是一个超级App.这篇通过ViewPager + Fragment实现一个类似于微信的界面,之前有用FragmentTabHost实现过类似界面,ViewPager的实现方式 ...

  2. sql server 2005 大数据量插入性能对比

    sql server 2005大数据量的插入操作 第一,写个存储过程,传入参数,存储过程里面是insert操作, 第二,用System.Data.SqlClient.SqlBulkCopy实例方法, ...

  3. servlet从xml提取数据登陆

    在xml文档下可以设置参数的初始值,在这里把他当成了数据库在用 <servlet> <description>This is the description of my J2E ...

  4. 浅谈 trie树 及其实现

    定义:又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构, 如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. 核心思想:是空间换时间.利用字符串的公共前缀来降低查询时间的开 ...

  5. [LeetCode OJ] Gas Station

    问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...

  6. amf0解释一下

    就简单记录一下省了以后忘了,amf0其实就几种数据格式的网络传输格式,比如数字,字符串,这些格式在传输的时候他给单独序列化了一下,主要支持以下这些: #define AMF0_NUMBER ((uin ...

  7. 用script实现内容显示,并使用json传输数据

    今天做一个项目,要求是div内为空 所有代码都写在<script>里面,<script>里面的文本用json传输.这个对我一个刚出校门,用div写了三年的页面的人来说真的好难, ...

  8. underscorejs-size学习

    2.24 size 2.24.1 语法: _.size(list) 2.24.2 说明: 返回列表的长度. 示例一:返回数组.对象.字符串的长度 //取数组的长度 var length length ...

  9. CSS实现图片在div a标签中水平垂直居中

    CSS实现图片在div a标签中水平垂直居中 <div class="demo"> <a href="#"> <img src=& ...

  10. Bootstrap 和 LESS

    Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...