algorithm notes: leetcode#252 meeting rooms

Problem


Solution


Analysis

Python implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class :
def canAttendMeetings(self, intervals):
"""
:type intervals: List[Interval]
:rtype: bool
"""
intervals.sort(key=lambda a: a.start)
for i in range(len(intervals)-1):
if intervals[i].end > intervals[i+1].start:
return False
return True

Java implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 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; }
* }
*/
class {
public boolean canAttendMeetings(Interval[] intervals) {
Arrays.sort(intervals, new Comparator<Interval>() {
public int compare(Interval i1, Interval i2){
return i1.start - i2.start;
}
});
for(int i = 0; i < intervals.length - 1; i++)
if(intervals[i].end > intervals[i+1].start)
return false;
return true;
}
}

Time complexity

O(nlogn).

Space complexity

O(1).


252. Meeting Rooms