赞
踩
Leetcode 253. 会议室 II
https://leetcode-cn.com/problems/meeting-rooms-ii/
给你一个会议时间安排的数组 intervals ,每个会议时间都会包括开始和结束的时间 intervals[i] = [starti, endi] ,为避免会议冲突,同时要考虑充分利用会议室资源,请你计算至少需要多少间会议室,才能满足这些会议安排。
示例 1:
输入:intervals = [[0,30],[5,10],[15,20]]
输出:2
示例 2:
输入:intervals = [[7,10],[2,4]]
输出:1
提示:
1 <= intervals.length <= 104
0 <= starti < endi <= 106
class Solution { public: int minMeetingRooms(vector<vector<int>>& intervals) { if (intervals.size() == 0) { return 0; } priority_queue<int, std::vector<int>, std::greater<int> > allocator; sort(intervals.begin(), intervals.end(), [](auto a, auto b) { return a[0] < b[0]; }); allocator.push(intervals[0][1]); for (int i = 1; i < intervals.size(); i++) { if (intervals[i][0] >= allocator.top()) { allocator.pop(); } allocator.push(intervals[i][1]); } return allocator.size(); } };
class Solution { public: int minMeetingRooms(vector<vector<int>>& intervals) { if (intervals.size() == 0) { return 0; } vector<int> starts(intervals.size(), 0); vector<int> ends(intervals.size(), 0); for (int i = 0; i < intervals.size(); i++) { starts[i] = intervals[i][0]; ends[i] = intervals[i][1]; } sort(starts.begin(), starts.end()); sort(ends.begin(), ends.end()); auto startIter = starts.begin(); auto endIter = ends.begin(); int useRooms = 0 ; while (startIter != starts.end()) { if (*startIter >= *endIter) { useRooms -= 1; endIter++; } useRooms += 1; startIter++; } return useRooms; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。