fromsortedcontainersimportSortedDictclassMyCalendar:def__init__(self):self.sd=SortedDict()defbook(self,start:int,end:int)->bool:idx=self.sd.bisect_right(start)if0<=idx<len(self.sd):ifend>self.sd.values()[idx]:returnFalseself.sd[end]=startreturnTrue# Your MyCalendar object will be instantiated and called as such:# obj = MyCalendar()# param_1 = obj.book(start,end)
importjava.util.Map;importjava.util.TreeMap;classMyCalendar{privatefinalTreeMap<Integer,Integer>tm=newTreeMap<>();publicMyCalendar(){}publicbooleanbook(intstart,intend){Map.Entry<Integer,Integer>ent=tm.floorEntry(start);if(ent!=null&&ent.getValue()>start){returnfalse;}ent=tm.ceilingEntry(start);if(ent!=null&&ent.getKey()<end){returnfalse;}tm.put(start,end);returntrue;}}/** * Your MyCalendar object will be instantiated and called as such: MyCalendar * obj = new MyCalendar(); boolean param_1 = obj.book(start,end); */
typeMyCalendarstruct{rbt*redblacktree.Tree}funcConstructor()MyCalendar{returnMyCalendar{rbt:redblacktree.NewWithIntComparator(),}}func(this*MyCalendar)Book(startint,endint)bool{ifp,ok:=this.rbt.Floor(start);ok&&p.Value.(int)>start{returnfalse}ifp,ok:=this.rbt.Ceiling(start);ok&&p.Key.(int)<end{returnfalse}this.rbt.Put(start,end)returntrue}/** * Your MyCalendar object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Book(start,end); */
classMyCalendar{privatevarcalendar:[(Int,Int)]init(){self.calendar=[]}funcbook(_start:Int,_end:Int)->Bool{letnewEvent=(start,end)letindex=calendar.firstIndex{$0.0>=newEvent.1}??calendar.countifindex>0&&calendar[index-1].1>newEvent.0{returnfalse}calendar.insert(newEvent,at:index)returntrue}}/** * Your MyCalendar object will be instantiated and called as such: * let obj = MyCalendar() * let ret = obj.book(start, end) */