another way that might be much simpler:
def meeting_rooms(intervals: List[List[int]]) → bool:
# WRITE YOUR BRILLIANT CODE HERE
intervals.sort()
for i in range(len(intervals)-1):
if intervals[i][1]>intervals[i+1][0]:
return False
return True
Given the test cases, if you don’t check for equality (e.g. interval1[1] <= interval2[0]), it’s going to pass. Add a test case for when one meeting has the same end time as the start time for another meeting, and make sure that this is clear in the problem description, that if you two meetings have matching (but not overlapping) start/end times, this would return false.
I think it should still return True cause it is not overlapping time slots and they can still attend both meetings back to back.
this is my solution as well.
Using a heap:
function meetingRooms(intervals) {
const heap = new Array(intervals.length).fill(null);
heap[0] = intervals[0];
for(let i = 1; i < intervals.length; i++) {
let root = 0;
while(heap[root]) {
if(intervals[i][0] < heap[root][0]) {
if(intervals[i][1] > heap[root][0]) return false;
const temp = intervals[i];
intervals[i] = heap[root];
heap[root] = temp;
root = 2 * root + 1;
} else {
if(heap[root][1] > intervals[i][0]) return false;
root = 2 * root + 2;
}
}
heap[root] = intervals[i];
}
return true;
}
Should classify with interviewer with the case of x2 == y1
def is_overlap(inv1, inv2):
return not (inv1[1] <= inv2[0] or inv2[1] <= inv1[0])
def meeting_rooms(intervals: List[List[int]]) -> bool:
intervals.sort()
n = len(intervals)
for i in range(n-1):
if is_overlap(intervals[i], intervals[i+1]):
return False
return True