资讯专栏INFORMATION COLUMN

[LeetCode] 253. Meeting Rooms II

mengera88 / 1613人阅读

Problem

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.

Example 1:

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
Example 2:

Input: [[7,10],[2,4]]
Output: 1

Solution Using Array

</>复制代码

  1. class Solution {
  2. public int minMeetingRooms(Interval[] intervals) {
  3. //0, 100 ---- 1
  4. //0, 200 ---- 2
  5. //0, 300 ---- 3
  6. //150, 250 -- 3
  7. //160, 260 -- 4
  8. //210, 310 -- 4
  9. int n = intervals.length;
  10. int[] startTimes = new int[n];
  11. int[] endTimes = new int[n];
  12. for (int i = 0; i < n; i++) {
  13. startTimes[i] = intervals[i].start;
  14. endTimes[i] = intervals[i].end;
  15. }
  16. Arrays.sort(startTimes);
  17. Arrays.sort(endTimes);
  18. int count = 0;
  19. int i = 0, j = 0;
  20. while (i < n && j < n) {
  21. //once overlaps, room++
  22. if (startTimes[i] < endTimes[j]) count++;
  23. //not overlapping, release prev meeting room
  24. else j++;
  25. i++;
  26. }
  27. return count;
  28. }
  29. }
Using Heap

</>复制代码

  1. class Solution {
  2. public int minMeetingRooms(Interval[] intervals) {
  3. if (intervals == null || intervals.length == 0) return 0;
  4. Arrays.sort(intervals, (a, b) -> a.start-b.start);
  5. PriorityQueue queue = new PriorityQueue<>((a, b) -> a.end-b.end);
  6. /*add room for the first meeting*/
  7. queue.add(intervals[0]);
  8. for (int i = 1; i < intervals.length; i++) {
  9. Interval pre = queue.poll();
  10. Interval cur = intervals[i];
  11. // if new meeting time overlapped, add a new room for this meeting
  12. if (cur.start < pre.end) queue.offer(cur);
  13. // if not overlapped, no new room for the new meeting,
  14. // just update the ending time for the earliest-to-end room
  15. else pre.end = cur.end;
  16. // put this previous earliest-to-end room back back to heap,
  17. // so that in next iteration, we get the new earliest-to-end room
  18. queue.offer(pre);
  19. }
  20. return queue.size();
  21. }
  22. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77272.html

相关文章

  • [Leetcode] Meeting Rooms 会议室

    摘要:排序法复杂度时间空间思路这题和很像,我们按开始时间把这些都给排序后,就挨个检查是否有冲突就行了。有冲突的定义是开始时间小于之前最晚的结束时间。这里之前最晚的结束时间不一定是上一个的结束时间,所以我们更新的时候要取最大值。 Meeting Rooms Given an array of meeting time intervals consisting of start and end...

    jubincn 评论0 收藏0
  • Meeting Rooms & Meeting Rooms II

    摘要:思路这道题就是要找区间之间是否有。而的复杂度是,所以最后总的复杂度为。思路的条件依然是不同的是这题需要求房间数。还是先,指向之前有的最小的那一个。接着的是,比小,所以又放入。。的是,比大,因此出,放入。。 Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[...

    TalkingData 评论0 收藏0
  • [LintCode/LeetCode] Meeting Rooms

    Problem 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. Example Given intervals = [[0,30],[...

    Noodles 评论0 收藏0
  • [Leetcode] Walls and Gates 墙与门

    摘要:广度优先搜索复杂度时间空间思路实际上就是找每个房间到最近的门的距离,我们从每个门开始,广度优先搜索并记录层数就行了。这题要注意剪枝,如果下一步是门或者下一步是墙或者下一步已经访问过了,就不要加入队列中。 Walls and Gates You are given a m x n 2D grid initialized with these three possible values....

    Edison 评论0 收藏0
  • [Leetcode] Best Meeting Point 最佳见面点

    摘要:为了尽量保证右边的点向左走,左边的点向右走,那我们就应该去这些点中间的点作为交点。由于是曼哈顿距离,我们可以分开计算横坐标和纵坐标,结果是一样的。 Best Meeting Point A group of two or more people wants to meet and minimize the total travel distance. You are given a ...

    王军 评论0 收藏0

发表评论

0条评论

mengera88

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<