看板 Marginalman
用self.time把start跟end都推進去(照順序單調遞增) 然後另外記下哪些是start時間 如果可以插入的話 bisect_right(start) 會等於 bisect_left(end) 但要注意單純符合這個條件不夠 因為可能會被另外一個事件完整包住 這樣這兩個也會相等 所以再加一個條件 就是找到的前一個index不能是start的時間 這樣就可以了 但這個判斷式當l=r=0時會out of bound 就另外注意 搞得有點複雜== class MyCalendar: def __init__(self): self.times = [] self.starts = set() def book(self, start: int, end: int) -> bool: l = bisect_right(self.times, start) r = bisect_left(self.times, end) if (l==0 and r==0) or (l==r and (self.times[l-1] not in self.starts)): self.times.insert(l, start) self.times.insert(l+1, end) self.starts.add(start) return True else: return False -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1727364500.A.51B.html ※ 編輯: DJYOSHITAKA (125.229.37.69 臺灣), 09/26/2024 23:29:35