LeetCode Meeting Rooms

原题链接在这里:https://leetcode.com/problems/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.

题解:

对array进行排序,从i = 1开始判断start是否在前一个end之前, 若是就return false. 完成loop返回true.

Time Complexity: O(nlogn). Space: O(1).

AC Java:

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if(intervals == null || intervals.length == 0){
return true;
}
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval i1, Interval i2){
if(i1.start == i2.start){
return i1.end - i2.end;
}
return i1.start - i2.start;
}
}); for(int i = 1; i<intervals.length; i++){
if(intervals[i].start < intervals[i-1].end){
return false;
}
}
return true;
}
}

跟上Meeting Rooms II

上一篇:给Macbook Pro更换固态硬盘并转移系统的最简单办法


下一篇:C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)