只要按start time sort然后看所有interval是不是两两不相交即可。时间复杂度O(n * log n),代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for an interval. | |
* struct Interval { | |
* int start; | |
* int end; | |
* Interval() : start(0), end(0) {} | |
* Interval(int s, int e) : start(s), end(e) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
bool canAttendMeetings(vector<Interval>& intervals) { | |
int len = intervals.size(); | |
auto comp = [](const Interval& lhs, const Interval& rhs) | |
{ | |
return lhs.start < rhs.start; | |
}; | |
sort(intervals.begin(), intervals.end(), comp); | |
for(int i = 0; i < len - 1; ++i) | |
{ | |
if(intervals[i].end > intervals[i + 1].start)return false; | |
} | |
return true; | |
} | |
}; |
No comments:
Post a Comment