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 ...
随机推荐
- winform只能有一个实例运行且打开已运行窗口
static class Program { private static Mutex onlyOne; [STAThread] static void Main() { onlyOne = new ...
- sn 密钥注册
::打开开发人员命令提示符输入一下内容与证书密码sn -i CanChou.snk.pfx VS_KEY_4B89A33EE2B53C07
- PHP isset 函数作用
isset函数是检测变量是否设置. 格式:bool isset ( mixed var [, mixed var [, ...]] ) 返回值: 若变量不存在则返回 FALSE 若变量存在且其值为NU ...
- MVC下分页的自定义分页一种实现
1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...
- 部署Redis for Windows服务
一.环境 Redis Windows 版本:2.8.2104 二.植入Windows服务 > redis-server.exe --service-install redis.window ...
- 命名不规范引发的DropDownListFor无法选中
问题的引出: 项目中遇到和以下链接相同的问题,大概可以描述为:后台ViewData["KeyName"],前台Model属性里也有一个字段KeyName,那么DropDownLis ...
- 看见了就转来了, 涉及到UBOOT 地址的一个问题.
addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);什么意思? 这是UBOOT 中的一个分配视频帧缓冲区地址的函数,我想问的是:加一个 ...
- JavaScript基础整理(2)
接下来的重点是函数.我们知道函数是特殊的对象. 函数作用域和声明提前.JavaScript中没有块级作用域,只有函数作用域:变量在声明它们的函数体以及这个函数体嵌套的任意 函数体内都要定义. func ...
- 安装使用ubuntu和opensuse
liquid: n.液体, adj. 液体的, 流动的 liquidate: v. 清洗; 清算; 变现; 杀戮; weird: [wi2d] e:i, ir:2 离奇的,古怪的... 英文名称, 直 ...
- [译]在Mac上运行ASP.NET 5
原文:http://stephenwalther.com/archive/2015/02/03/asp-net-5-and-angularjs-part-7-running-on-a-mac 这篇文章 ...