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

[LeetCode] Meeting Rooms I & II

时间:2015-09-06 12:33:19      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

Meeting Rooms

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.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.

 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool canAttendMeetings(vector<Interval>& intervals) {
13         sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b) {
14             return a.start < b.start;
15         });
16         for (int i = 1; i < intervals.size(); ++i) {
17             if (intervals[i].start < intervals[i-1].end) return false;
18         }
19         return true;
20     }
21 };

 

 

Meeting Rooms II

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.

 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int minMeetingRooms(vector<Interval>& intervals) {
13         vector<pair<int, int>> schedule;
14         for (auto interval : intervals) {
15             schedule.push_back({interval.start, 1});
16             schedule.push_back({interval.end, -1});
17         }
18         sort(schedule.begin(), schedule.end());
19         int cnt = 0, res = 0;
20         for (auto s : schedule) {
21             if (s.second == 1) ++cnt;
22             else --cnt;
23             res = max(res, cnt);
24         }
25         return res;
26     }
27 };

 

[LeetCode] Meeting Rooms I & II

标签:

原文地址:http://www.cnblogs.com/easonliu/p/4785020.html

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