Facebook面試題 meeting rooms 求同時最多meeting room的時間
給出會議開始和結束時間,求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;
}
}
相關文章
- Meeting Rooms IIOOM
- 252. Meeting RoomsOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- 253. Meeting Rooms IIOOM
- Leetcode: Meeting RoomsLeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- Leetcode 253: meeting roomLeetCodeOOM
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- OUTLOOK - Unable to Delete Meetingsdelete
- netmeeting命令conf
- Daily Stand-up Meeting的反饋AI
- LeetCode-Best Meeting PointLeetCode
- AICreateMeeting-遇見未來AI
- Netmeeting 安裝步驟
- NetMeeting高階應用 (轉)
- 每天站立會議(Daily Stand-up Meeting)AI