Sunday, August 6, 2017

[Leetcode] Meeting Rooms, Solution

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.

[Thoughts]
把所有的interval按照开始时间来排序,然后比较相邻的interval是否有重叠即可。

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


No comments: