Welcome to Subscribe On Youtube
252. Meeting Rooms
Description
Given an array of meeting time intervals
where intervals[i] = [starti, endi]
, determine if a person could attend all meetings.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]] Output: false
Example 2:
Input: intervals = [[7,10],[2,4]] Output: true
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106
Solutions
-
Sort the Intervals: First, the list of
intervals
is sorted based on the start times of the meetings. Sorting is crucial because it allows for a linear comparison of consecutive meetings to check for any overlaps. The sorting uses a lambda function as the key, which sorts the intervals by their start time (x[0]
).intervals.sort(key=lambda x: x[0])
-
Iterate Through Sorted Intervals: The method then iterates through the sorted intervals, except for the last one (hence
len(intervals) - 1
). This is because the comparison is always made between the current interval and the next one, and there’s no next interval for the last one.for i in range(len(intervals) - 1):
-
Check for Overlapping Intervals: For each pair of consecutive intervals, the code checks if the end time of the current interval (
intervals[i][1]
) is greater than the start time of the next interval (intervals[i + 1][0]
). If this condition is true for any pair of intervals, it means there’s a conflict (one meeting does not end before the next one starts), and the person cannot attend all meetings. In such a case, the method immediately returnsFalse
.if intervals[i][1] > intervals[i + 1][0]: return False
-
Return True if No Conflicts Found: If the method iterates through all interval pairs without finding any overlaps, it means all meetings can be attended without conflicts. Thus, the method returns
True
.return True
Example:
Given the input intervals = [[0, 30], [5, 10], [15, 20]]
:
- After sorting,
intervals = [[0, 30], [5, 10], [15, 20]]
. - The method finds that the first interval
[0, 30]
overlaps with the next interval[5, 10]
since30 > 5
, and it returnsFalse
.
-
class Solution { public boolean canAttendMeetings(int[][] intervals) { Arrays.sort(intervals, (a, b) -> a[0] - b[0]); for (int i = 1; i < intervals.length; ++i) { var a = intervals[i - 1]; var b = intervals[i]; if (a[1] > b[0]) { return false; } } return true; } }
-
class Solution { public: bool canAttendMeetings(vector<vector<int>>& intervals) { sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) { return a[0] < b[0]; }); for (int i = 1; i < intervals.size(); ++i) { if (intervals[i][0] < intervals[i - 1][1]) { return false; } } return true; } };
-
class Solution: def canAttendMeetings(self, intervals: List[List[int]]) -> bool: intervals.sort() return all(a[1] <= b[0] for a, b in pairwise(intervals)) ############ from itertools import pairwise class Solution: def canAttendMeetings(self, intervals: List[List[int]]) -> bool: intervals.sort(key=lambda x: x[0]) return not any(a[1] > b[0] for a,b in pairwise(intervals)) # any is True, then conflict found ############ class Solution: def canAttendMeetings(self, intervals: List[List[int]]) -> bool: intervals.sort(key=lambda x: x[0]) for i in range(len(intervals) - 1): if intervals[i][1] > intervals[i + 1][0]: return False return True ############ # Definition for an interval. # class Interval(object): # def __init__(self, s=0, e=0): # self.start = s # self.end = e class Solution(object): def canAttendMeetings(self, intervals): """ :type intervals: List[Interval] :rtype: bool """ intervals = sorted(intervals, key=lambda x: x.start) for i in range(1, len(intervals)): if intervals[i].start < intervals[i - 1].end: return False return True
-
func canAttendMeetings(intervals [][]int) bool { sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] }) for i := 1; i < len(intervals); i++ { if intervals[i][0] < intervals[i-1][1] { return false } } return true }
-
function canAttendMeetings(intervals: number[][]): boolean { intervals.sort((a, b) => a[0] - b[0]); for (let i = 1; i < intervals.length; ++i) { if (intervals[i][0] < intervals[i - 1][1]) { return false; } } return true; }
-
impl Solution { #[allow(dead_code)] pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool { if intervals.len() == 1 { return true; } let mut intervals = intervals; // Sort the intervals vector intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); let mut end = -1; // Begin traverse for p in &intervals { if end == -1 { // This is the first pair end = p[1]; continue; } if p[0] < end { return false; } end = p[1]; } true } }