🟩 Meeting Rooms (#252) 🔒
🔗 Problem Link
📋 Problem Statement
Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.
Note: This is a premium LeetCode problem, but available for free on NeetCode.
💡 Examples
Example 1
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Example 2
Input: intervals = [[7,10],[2,4]]
Output: true
🔑 Key Insights & Approach
Core Observation: Sort by start time, check if any meetings overlap.
Why Sort?
- O(n log n) time
- Linear scan after sorting
- Overlaps if start[i] < end[i-1]
Pattern: "Interval Overlap Detection" pattern.
🐍 Solution: Python
Approach: Sort and Check
Time Complexity: O(n log n) | Space Complexity: O(1)
from typing import List
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
if not intervals:
return True
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i-1][1]:
return False
return True
🔵 Solution: Golang
Approach: Sort and Check
Time Complexity: O(n log n) | Space Complexity: O(1)
import "sort"
func canAttendMeetings(intervals [][]int) bool {
if len(intervals) == 0 {
return true
}
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
}