Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Solution: My Calendar I
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
class Solution: def book(self, nums): result = [] # to track the booked times b_start, b_end = float('inf'), float('-inf') for start, end in nums: if start >= b_end or end <= b_start: b_start = min(start, b_start) b_end = max(b_end, end) result.append(True) else: result.append(False) return result
A
Anonymous
· 2 years ago
import bisect from typing import List class Solution: def __init__(self): self.bookings = [] def book(self, nums: List[List[int]]) -> List[bool]: results = [] for interval in nums: start, end = interval event = (start, end) # Find the index where the event should be inserted to maintain sorted order idx = bisect.bisect_left(self.bookings, event) # Find lower and higher neighbors lower = self.bookings[idx - 1] if idx > 0 else None higher = self.bookings[idx] if idx < len(self.bookings) else None # Check for overlap with neighboring events if (lower is None or lower[1] <= start) and (higher is None or end <= higher[0]):
Reading Progress
0%