Meeting Rooms II -- LeetCode
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相关推荐
- LeetCode 252. Meeting Rooms
原题链接在这里:https://leetcode.com/problems/meeting-rooms/ 题目: Given an array of meeting time intervals co ...
- 252.Meeting Rooms
/** 252.Meeting Rooms* 2016-6-20 by Mingyang* Arrays.sort的用法要熟悉* 这题和Merge Intervals很像,我们按开始时间把这些Inte ...
- LeetCode 252. Meeting Rooms (会议室)$
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- Leetcode 252, 253. Meeting Rooms
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [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 ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- Permutations I II leetcode
Permutations I Given a collection of distinct numbers, return all possible permutations. For example ...
- LeetCode 59. 螺旋矩阵 II LeetCode 54. 螺旋矩阵
文章目录 1. 题目信息 2. LeetCode 59 解题 3. LeetCode 54. 螺旋矩阵 4.<剑指Offer>面试题29 1. 题目信息 给定一个正整数 n,生成一个包含 ...
- 贪心——跳跃游戏 II(Leetcode 45)
题目选自 Leetcode 45. 跳跃游戏 || 与跳跃游戏 | 的不同之处在于,我们需要求的是最少的跳跃次数~ 题目描述: 解题思路: 思想 就一句话:每次在上次能跳到的范围(end)内选择一个能 ...
最新文章
- web安全之token
- 利用mem数组在MM32 MicroPython中实现COMP的功能
- 【深度学习入门到精通系列】什么是消融实验(Ablation experiment)
- OpenGL 期末考试作业
- curl 发送带有Authorization的post请求命令
- OpenExpressApp对建模支持的初步计划
- sublime3 常用功能总结
- 音视频同步原理[ffmpeg]
- DocDokuPLM介绍
- 【论文】本周论文推荐(迁移学习、阅读理解、对话系统、图神经网络、对抗生成网络等)...
- Python装饰器、metaclass、abc模块学习笔记
- vue require图片_前后端分离当下,后端同学总结的手动构建vue项目
- 数据库服务器型号类型,数据库服务器型号
- bin文件python读取
- 手写数字识别mnist
- 我是怎样开发一个开源系统的安全模块?
- C#数据库教程2-ADO.NET常用SQL语句
- macos 版本_如何检查您使用的macOS版本
- OpenCv相机标定——圆形标定板标定
- Binwalk的安装和使用