main algorithms/lessons/09-mock-interview/mock_interview.py

17 lines574 B

"""Question: given a list of meeting times, can one person attend them all?"""
def can_attend_naive(meetings): # O(n^2): compare every pair
for i, (s1, e1) in enumerate(meetings):
for s2, e2 in meetings[i + 1:]:
if s1 < e2 and s2 < e1:
return False
return True
def can_attend(meetings): # O(n log n): sort, then check neighbours
meetings = sorted(meetings)
return all(prev[1] <= cur[0] for prev, cur in zip(meetings, meetings[1:]))
print(can_attend([(9, 10), (10, 11), (13, 14)]), can_attend([(9, 12), (11, 13)]))