meeting room I & II
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.
这题和求解有多少架飞机在空中一样。
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
List<TimePoint> list = new ArrayList<TimePoint>(); for (Interval interval : intervals) {
list.add(new TimePoint(interval.start, true));
list.add(new TimePoint(interval.end, false));
} Collections.sort(list, new Comparator<TimePoint>() {
public int compare(TimePoint t1, TimePoint t2) {
if (t1.time < t2.time) {
return -;
} else if (t1.time > t2.time) {
return ;
} else {
if (t1.isStart) {
return ;
} else {
return -;
}
}
}
});
int count = ;
for (TimePoint t : list) {
if (t.isStart) {
count++;
if (count == ) return false;
} else {
count--;
}
}
return true;
}
} class TimePoint {
int time;
boolean isStart; public TimePoint(int time, boolean isStart) {
this.time = time;
this.isStart = isStart;
}
}
网上看到一个更简单的方法:先sort intervals, 然后看俩个相邻的点是否有重合。
public boolean canAttendMeetings(Interval[] intervals) {
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b){
return a.start-b.start;
}
}); for(int i=; i<intervals.length-; i++){
if(intervals[i].end>intervals[i+].start){
return false;
}
}
return true;
}
Meeting Rooms 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
.
方法和第一种相似。
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
List<TimePoint> list = new ArrayList<TimePoint>(); for (Interval interval : intervals) {
list.add(new TimePoint(interval.start, true));
list.add(new TimePoint(interval.end, false));
} Collections.sort(list, (t1, t2) -> {
if (t1.time < t2.time) {
return -;
} else if (t1.time > t2.time) {
return ;
} else {
if (t1.isStart) {
return ;
} else {
return -;
}
}
}); int count = , max = ;
for (TimePoint t : list) {
if (t.isStart) {
count++;
max = Math.max(count, max);
} else {
count--;
}
}
return max;
}
} class TimePoint {
int time;
boolean isStart; public TimePoint(int time, boolean isStart) {
this.time = time;
this.isStart = isStart;
}
}
follow up: group all meetings by meeting rooms. 我的解法是对所有会议急于start排序,然后按顺序的分组搞定。
meeting room I & II的更多相关文章
- [Locked] Meeting Room I && II
Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...
- [LeetCode] Meeting Rooms I & II
Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s ...
- 边工作边刷题:70天一遍leetcode: day 84-3
Meeting Rooms I/II 要点:这题和skyline类似,利用了interval start有序的特点,从左向右处理,用一个heap来动态表示当前占用rooms的时间段,所以heap的si ...
- BugPhobia开发篇章:Beta阶段第II次Scrum Meeting
0x01 :Scrum Meeting基本摘要 Beta阶段第二次Scrum Meeting 敏捷开发起始时间 2015/12/13 00:00 A.M. 敏捷开发终止时间 2015/12/14 22 ...
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode Meeting Rooms II
原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...
- 253. Meeting Rooms II
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
- [LeetCode#253] Meeting Rooms II
Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...
- [Swift]LeetCode253.会议室 II $ Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
随机推荐
- cookie和session的对比
1.存放的位置 cookie存在客户端的临时文件夹 session:存在服务器的内存中,一个session域对象为一个用户浏览器服务. 2.安全性 cookie是以明文方式存放在客 ...
- yii2嵌入微信公众号支付
原文地址:https://segmentfault.com/a/1190000005114556
- Samus驱动中的Document条件
今天要说一个东西就是Samus驱动里的 Document 和他的一个子类 Op 在Samus驱动的增删改查方法中都有这类的参数传递.. 大致的使用方法是这样.. MongoU.Find<Per ...
- 弹出框四 之toastr.js (完成提示框)
1.下载 toastr.js组件 2. $(function () { toastr.success('提交数据成功'); toastr.error('Error'); toastr.warning( ...
- 如何在本地配置php分析工具xhprof
测试环境: linuxMint + nginx1.4.6+mysql5.5+php5.5 什么是xhprof? XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括阻塞时 ...
- 关于shell脚本时遇value too great for base (error token is "08")
今天在书写一个定时cp脚本时遇到了一个问题,value too great for base (error token is "08") 在网上查看到原来是以0开头的数字 系统会默 ...
- 数字格式化函数:Highcharts.numberFormat()
(转)数字格式化函数:Highcharts.numberFormat() 一.函数说明 该函数用于图表中数值的格式化,常见用途有数值精度控制.小数点符.千位符显示控制等. 二.函数使用 1.函 ...
- ELK日志分析系统搭建(转)
摘要: 前段时间研究的Log4j+Kafka中,有人建议把Kafka收集到的日志存放于ES(ElasticSearch,一款基于Apache Lucene的开源分布式搜索引擎)中便于查找和分析,在研究 ...
- 安装wampserver 2.5的时候出现丢失MSVCR100.dll的解决办法。
转载地址:http://www.mafutian.net/127.html
- ThinkPHP报错处理
1,当运行结果提示:找不到该页面(控制器),怎么办? 建造一个空页面:EmptyController <?php namespace Home\Controller; use Think\Con ...