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 ...
随机推荐
- 微信jssdk录音功能开发记录
原文地址:http://www.cnblogs.com/liujunyang/p/4962423.html
- 未能加载文件或程序集“MySQLDriverCS
未能加载文件或程序集“MySQLDriverCS, Version=3.0.1735.36021, Culture=neutral, PublicKeyToken=172f94dfb0faf263”或 ...
- Android Studio-设置放大代码编辑区
原有的快捷键是ctrl+shift+F12,现在我修改成了Ctrl+M.
- django 文件上传
模板文件: <form method='post' action='/script/upload/' enctype="multipart/form-data" accept ...
- sql移除换行回车符号 \r\n
--移除回车符 update master_location SET street_number = REPLACE(street_number, CHAR(13), '') --移除换行符 upda ...
- [译]Mongoose指南 - Connection
使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...
- Outlook不能打开附件(提示:无法创建文件xx,请右键单击要在其中创建文件的文件夹..)
问题分析: 出现这种问题的几率很小,除非你是每天都需要使用Outlook的办公人员.出现这种问题我想有如下两种可能.1.注册表中指定的附档临时保存的目录没有写入的相关权限.2.同名附档已存在且权限出现 ...
- Google Protocol Buffer 简单介绍
以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...
- PHP的 Mysqli扩展库的多语句执行
$mysqli->multi_query($sqls); 执行多个sql语句,返回true/false 有结果集时,使用 $mysqli->store_result(); 来获取结 ...
- springmvc之log4j
1.工程结构 2.所需jar包 3.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...