Facebook面试题 meeting rooms 求同时最多meeting room的时间
来源:互联网 发布:mysql tar.gz安装 编辑:程序博客网 时间:2024/06/04 01:37
给出会议开始和结束时间,求meeting room interval最多的时间点。返回任意一个时间即可。
- 显然本题可以方便地使用扫描线算法解决。在扫描线段的同时保存一个时间。最终返回最多interval对应的时间即可。整个算法可以描述如下:
- 将meeting开始和结束的时间点分别存在list中。
- 对整个list按时间排序。相同的时间结束应该优先于开始。
- 遍历排序好的list,对同时存在的线段进行计数并记录时间。
O(nlogn)
时间,O(n)
空间。因为我们最多对2n
个点进行了存储,排序和遍历。
算法的Java实现如下:
/** * Definition of Interval: * public class Interval { * int start, end; * Interval(int start, int end) { * this.start = start; * this.end = end; * } * } */class Point { int time; int flag; public Point(int time, int flag) { this.time = time; //end flag = 0, start flag = 1 this.flag = flag; } public static Comparator<Point> PointComparator = new Comparator<Point>() { @Override public int compare(Point p1, Point p2) { if (p1.time == p2.time) { return p1.flag - p2.flag; // end has priority over start } return p1.time - p2.time; } };}class Solution { public int timeWithMostIntervals(List<Interval> meetings) { if (meetings == null || meetings.size() == 0) { return 0; } List<Point> lines = new ArrayList<meetings.size() * 2>(); for (Interval i : meetings) { lines.add(i.start, 1); lines.add(i.end, 0); } Collections.sort(lines, Point.PointComparator); int ret = 0; int max = Integer.MIN_VALUE; int count = 0; for (int i = 0; i < lines.size(); i++) { if (lines.get(i).flag == 0) { count--; } else { count++; } if (count > max) { max = count; ret = lines.get(i).time; } } return ret; }}
0 0
- Facebook面试题 meeting rooms 求同时最多meeting room的时间
- [Facebook 面试题]Meeting roomII 变形题
- Meeting Rooms
- Meeting Rooms
- Meeting Rooms
- Meeting Rooms
- Leetcode: Meeting Rooms
- [LeetCode 252] Meeting Rooms
- [leetcode 252] Meeting Rooms
- *LeetCode-Meeting Rooms II
- [LeetCode252]Meeting Rooms
- [LeetCode253]Meeting Rooms II
- Leetcode-252.Meeting Rooms
- Leetcode 252Meeting Rooms
- LeetCode 252. Meeting Rooms
- Meeting Rooms II
- Leetcode 252 meeting rooms
- Meeting Rooms II
- 【Java】静态代理
- git 上传本地项目到github以及遇到的一些问题
- Html: 表单、文本域、单选按钮
- mongodb的安装和简单使用
- python-opencv 直方图目标区域匹配
- Facebook面试题 meeting rooms 求同时最多meeting room的时间
- Objective-c - 内存管理(ARC/MRC),第一个MRC程序
- 批量导入
- 二叉排序树的java实现
- 给定两个int a和b,不使用if-else等比较和判断运算符,请返回较大的一个数。若两数相同则返回任意一个
- input等的data-name、data-age HTML5 自定义属性
- 【Java】动态代理
- 使用 jquery 的 ajax 进行接口化开发(获取验证码,注册)
- 架构师的必备素质和成长途径