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 ...
随机推荐
- HTTP1.0与HTTP1.1的区别
HTTP/1.1与HTTP/1.0的区别 下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的差别,当然,更多的内容是放在解释这种差异背后的机制上. 1 可扩展性 可扩展性的一个重要原 ...
- 微信jssdk录音功能开发记录
原文地址:http://www.cnblogs.com/liujunyang/p/4962423.html
- yii2 Pjax的使用
有两个例子:刷新时间和数据显示排序 1.刷新时间 (1)控制器中的方法:Time public function actionTime() { return $this->render('tim ...
- MySQL索引之前缀索引和索引选择性
有时需要索引很长的字符列,它会使索引变大而且变慢.一个策略就是模拟哈希索引.但是有时这也不够好,那? 通常可以索引开始的几个字符,而不是全部值,以节约空间并得到好的性能.这使索引需要的空间变小,但是也 ...
- Android Studio-设置鼠标悬停显示方法声明
- 软件安装失败,导致ubuntu软件中心软件消失
感谢百度上各位IT界朋友的帮助,由于某个软件安装失败,导致ubuntu软件中心软件消失的解决办法: 找百度,有人说, 使用命令:sudo apt-get install software-center ...
- typecho除了首页其他大部分网页404怎么办?
server { listen ; server_name blog.localhost; #绑定域名 index index.htm index.html index.php; #默认文件 root ...
- hdu4951 Multiplication table (乘法表的奥秘)
http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...
- 【Bootstrap】Bootstrap和Java分页-第二篇
目录 关于此文 配置xml-pager.tld 分页控件-Pager 分页action集成类-BaseController 实例-Dao 实例-service 实例-action 实例-JSP 实例- ...
- Swift编程语言资料合集
在本周二凌晨召开的苹果年度开发者大会WWDC上,苹果公司推出了全新的编程语言Swift.Swift 基于C和Objective-C,是供iOS和OS X应用编程的全新语言,更加高效.现代.安全,可以提 ...