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.

思路:贪心。

将所有interval按照开始时间从早到晚排序。之后依次安排会议室。当我们考虑一个interval时,查看下当前已经被分配了会议室的时间,如果其中最早的结束时间要早于当前的interval的开始时间,则把该房间分配给当前的interval,否则增加一个会议室。实现时我们可以用最小堆或者优先队列来实现。然后期间会议室数量的最大值就是结果。

时间复杂度为O(NlogN)。

/*** Definition for an interval.* struct Interval {*     int start;*     int end;*     Interval() : start(0), end(0) {}*     Interval(int s, int e) : start(s), end(e) {}* };*/
class Solution {
public:int minMeetingRooms(vector<Interval>& intervals) {typedef pair<int, int> interval;vector<interval> meetings;for (int i = 0; i < intervals.size(); i++)meetings.push_back(make_pair(intervals[i].start, intervals[i].end));sort(meetings.begin(), meetings.end(), less<interval>());priority_queue<int, vector<int>, greater<int>> q;int res = 0;for (int i = 0; i < meetings.size(); i++) {int end = meetings[i].second;if (!q.empty() && q.top() <= meetings[i].first) q.pop();q.push(end);res = std::max(res, (int)q.size());}return res;}
};

转载于:https://www.cnblogs.com/fenshen371/p/5825494.html

Meeting Rooms II -- LeetCode相关推荐

  1. LeetCode 252. Meeting Rooms

    原题链接在这里:https://leetcode.com/problems/meeting-rooms/ 题目: Given an array of meeting time intervals co ...

  2. 252.Meeting Rooms

    /** 252.Meeting Rooms* 2016-6-20 by Mingyang* Arrays.sort的用法要熟悉* 这题和Merge Intervals很像,我们按开始时间把这些Inte ...

  3. LeetCode 252. Meeting Rooms (会议室)$

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. Leetcode 252, 253. Meeting Rooms

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

    115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...

  6. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  7. Permutations I II leetcode

    Permutations I Given a collection of distinct numbers, return all possible permutations. For example ...

  8. LeetCode 59. 螺旋矩阵 II LeetCode 54. 螺旋矩阵

    文章目录 1. 题目信息 2. LeetCode 59 解题 3. LeetCode 54. 螺旋矩阵 4.<剑指Offer>面试题29 1. 题目信息 给定一个正整数 n,生成一个包含 ...

  9. 贪心——跳跃游戏 II(Leetcode 45)

    题目选自 Leetcode 45. 跳跃游戏 || 与跳跃游戏 | 的不同之处在于,我们需要求的是最少的跳跃次数~ 题目描述: 解题思路: 思想 就一句话:每次在上次能跳到的范围(end)内选择一个能 ...

最新文章

  1. web安全之token
  2. 利用mem数组在MM32 MicroPython中实现COMP的功能
  3. 【深度学习入门到精通系列】什么是消融实验(Ablation experiment)
  4. OpenGL 期末考试作业
  5. curl 发送带有Authorization的post请求命令
  6. OpenExpressApp对建模支持的初步计划
  7. sublime3 常用功能总结
  8. 音视频同步原理[ffmpeg]
  9. DocDokuPLM介绍
  10. 【论文】本周论文推荐(迁移学习、阅读理解、对话系统、图神经网络、对抗生成网络等)...
  11. Python装饰器、metaclass、abc模块学习笔记
  12. vue require图片_前后端分离当下,后端同学总结的手动构建vue项目
  13. 数据库服务器型号类型,数据库服务器型号
  14. bin文件python读取
  15. 手写数字识别mnist
  16. 我是怎样开发一个开源系统的安全模块?
  17. C#数据库教程2-ADO.NET常用SQL语句
  18. macos 版本_如何检查您使用的macOS版本
  19. OpenCv相机标定——圆形标定板标定
  20. Binwalk的安装和使用

热门文章

  1. win10蓝牙不能用
  2. docker配置加速器,快速拉取镜像
  3. 【vite+pinia】
  4. 2023华为机考刷题指南:八周机考速通车
  5. web前端面试题完美整理/涵盖html,CSS、JS、浏览器、Vue、React、移动web。
  6. 通达信资金净流入公式_通达信成交额资金净流入指标公式
  7. 基于NLP的软件安全研究(二)
  8. 电容外观检测(牛角电容瑕疵缺陷视觉检测系统)
  9. 小米手机MIUI关闭广告大全
  10. H3C、思科路由器简单配置