码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Meeting Rooms II

时间:2015-08-08 14:58:54      阅读:690      评论:0      收藏:0      [点我收藏+]

标签:

The idea is to group those non-overlapping meetings in the same room and then count how many rooms we need. You may refer to this link.

The code is as follows.

 1 class Solution {
 2 public:
 3     int minMeetingRooms(vector<Interval>& intervals) {
 4         sort(intervals.begin(), intervals.end(), compare);
 5         vector<vector<Interval>> rooms;
 6         int n = intervals.size();
 7         for (int i = 0; i < n; i++) {
 8             int idx = findNonOverlapping(rooms, intervals[i]);
 9             if (rooms.empty() || idx == -1)
10                 rooms.push_back({intervals[i]});
11             else rooms[idx].push_back(intervals[i]);
12         }
13         return (int)rooms.size();
14     }
15 private:
16     static bool compare(Interval& interval1, Interval& interval2) {
17         return interval1.start < interval2.start;
18     }
19     int findNonOverlapping(vector<vector<Interval>>& rooms, Interval& interval) {
20         int n = rooms.size();
21         for (int i = 0; i < n; i++)
22             if (interval.start >= rooms[i].back().end)
23                 return i;
24         return -1;
25     }
26 };

 

[LeetCode] Meeting Rooms II

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4713099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!