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

[LeetCode] Meeting Rooms

时间:2015-08-08 14:48:20      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

The idea is pretty simple: first we sort the intervals in the ascending order of start; then we check for the overlapping of each pair of neighboring intervals. If they do, then return false; after we finish all the checks and have not returned false, just return true.

Sorting takes O(nlogn) time and the overlapping checks take O(n) time, so this idea isO(nlogn) time in total.

The code is as follows.

 1 class Solution {
 2 public:
 3     bool canAttendMeetings(vector<Interval>& intervals) {
 4         sort(intervals.begin(), intervals.end(), compare);
 5         int n = intervals.size();
 6         for (int i = 0; i < n - 1; i++)
 7             if (overlap(intervals[i], intervals[i + 1]))
 8                 return false;
 9         return true;
10     }
11 private:
12     static bool compare(Interval& interval1, Interval& interval2) {
13         return interval1.start < interval2.start;
14     }
15     bool overlap(Interval& interval1, Interval& interval2) {
16         return interval1.end > interval2.start;
17     } 
18 };

 

[LeetCode] Meeting Rooms

标签:

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

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