LC 252 - Meeting Rooms
LC 252 - Meeting Rooms
Question
Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.
Example 1:
1
2
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Example 2:
1
2
Input: intervals = [[7,10],[2,4]]
Output: true
Constraints:
0 <= intervals.length <= 104intervals[i].length == 20 <= starti < endi <= 106
Links
Question here and solution here
Solution
concept
This question used the same concept from 435. Non-overlapping Intervals to check for overlapping intervals.
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
if not intervals:
return True
intervals.sort()
prev_end = intervals[0][1]
for start, end in intervals[1:]:
if start < prev_end:
return False
else:
prev_end = end
return True
class NeetSolution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
intervals.sort(key=lambda i: i[0])
for i in range(1, len(intervals)):
i1 = intervals[i - 1]
i2 = intervals[i]
if i1[1] > i2[0]:
return False
return True
Complexity
time: $O(n \log n)$
space: $O(n)$
This post is licensed under CC BY 4.0 by the author.