[LeetCode] Meeting Rooms I & II
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 };
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 };