You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime.
Implement the MyCalendarTwo class:
MyCalendarTwo() Initializes the calendar object.
boolean book(int startTime, int endTime) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
Example 1:
Input
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, true, true, true, false, true, true]
Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
Constraints:
0 <= start < end <= 109
At most 1000 calls will be made to book.
Solutions
Solution 1: Difference Array
We can use the concept of a difference array to record the booking status at each time point. Then, we traverse all the time points and count the booking status at the current time point. If the number of bookings exceeds $2$, we return $\textit{false}$. Otherwise, we return $\textit{true}$.
The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the number of bookings.
1 2 3 4 5 6 7 8 9101112131415161718192021
classMyCalendarTwo:def__init__(self):self.sd=SortedDict()defbook(self,startTime:int,endTime:int)->bool:self.sd[startTime]=self.sd.get(startTime,0)+1self.sd[endTime]=self.sd.get(endTime,0)-1s=0forvinself.sd.values():s+=vifs>2:self.sd[startTime]-=1self.sd[endTime]+=1returnFalsereturnTrue# Your MyCalendarTwo object will be instantiated and called as such:# obj = MyCalendarTwo()# param_1 = obj.book(startTime,endTime)
classMyCalendarTwo{privatefinalMap<Integer,Integer>tm=newTreeMap<>();publicMyCalendarTwo(){}publicbooleanbook(intstartTime,intendTime){tm.merge(startTime,1,Integer::sum);tm.merge(endTime,-1,Integer::sum);ints=0;for(intv:tm.values()){s+=v;if(s>2){tm.merge(startTime,-1,Integer::sum);tm.merge(endTime,1,Integer::sum);returnfalse;}}returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo obj = new MyCalendarTwo(); * boolean param_1 = obj.book(startTime,endTime); */
classMyCalendarTwo{public:MyCalendarTwo(){}boolbook(intstartTime,intendTime){++m[startTime];--m[endTime];ints=0;for(auto&[_,v]:m){s+=v;if(s>2){--m[startTime];++m[endTime];returnfalse;}}returntrue;}private:map<int,int>m;};/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(startTime,endTime); */
typeMyCalendarTwostruct{rbt*redblacktree.Tree[int,int]}funcConstructor()MyCalendarTwo{returnMyCalendarTwo{rbt:redblacktree.New[int,int]()}}func(this*MyCalendarTwo)Book(startTimeint,endTimeint)bool{merge:=func(x,vint){c,_:=this.rbt.Get(x)ifc+v==0{this.rbt.Remove(x)}else{this.rbt.Put(x,c+v)}}merge(startTime,1)merge(endTime,-1)s:=0for_,v:=rangethis.rbt.Values(){s+=vifs>2{merge(startTime,-1)merge(endTime,1)returnfalse}}returntrue}/** * Your MyCalendarTwo object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Book(startTime,endTime); */
classMyCalendarTwo{privatetm:Record<number,number>={};constructor(){}book(startTime:number,endTime:number):boolean{this.tm[startTime]=(this.tm[startTime]??0)+1;this.tm[endTime]=(this.tm[endTime]??0)-1;lets=0;for(constvofObject.values(this.tm)){s+=v;if(s>2){if(--this.tm[startTime]===0){deletethis.tm[startTime];}if(++this.tm[endTime]===0){deletethis.tm[endTime];}returnfalse;}}returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */
varMyCalendarTwo=function(){this.tm={};};/** * @param {number} startTime * @param {number} endTime * @return {boolean} */MyCalendarTwo.prototype.book=function(startTime,endTime){this.tm[startTime]=(this.tm[startTime]||0)+1;this.tm[endTime]=(this.tm[endTime]||0)-1;lets=0;for(constvofObject.values(this.tm)){s+=v;if(s>2){if(--this.tm[startTime]===0){deletethis.tm[startTime];}if(++this.tm[endTime]===0){deletethis.tm[endTime];}returnfalse;}}returntrue;};/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */
Solution 2: Segment Tree
A segment tree divides the entire interval into multiple non-contiguous subintervals, with the number of subintervals not exceeding $\log(\textit{width})$. To update the value of an element, only $\log(\textit{width})$ intervals need to be updated, and these intervals are all contained within a larger interval that includes the element. When modifying intervals, a lazy mark is used to ensure efficiency.
Each node of the segment tree represents an interval;
The segment tree has a unique root node representing the entire statistical range, such as $[1, N]$;
Each leaf node of the segment tree represents a unit interval of length 1, $[x, x]$;
For each internal node $[l, r]$, its left child is $[l, \textit{mid}]$ and its right child is $[\textit{mid} + 1, r]$, where $\textit{mid} = \lfloor(l + r) / 2\rfloor$ (i.e., floor division).
For this problem, the segment tree nodes maintain the following information:
The maximum number of bookings within the interval $v$
Lazy mark $\textit{add}$
Since the time range is $10^9$, which is very large, we use dynamic node creation.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of bookings.
classNode:def__init__(self,l:int,r:int):self.left=Noneself.right=Noneself.l=lself.r=rself.mid=(l+r)>>1self.v=0self.add=0classSegmentTree:def__init__(self):self.root=Node(1,10**9+1)defmodify(self,l:int,r:int,v:int,node:Node=None):ifl>r:returnifnodeisNone:node=self.rootifnode.l>=landnode.r<=r:node.v+=vnode.add+=vreturnself.pushdown(node)ifl<=node.mid:self.modify(l,r,v,node.left)ifr>node.mid:self.modify(l,r,v,node.right)self.pushup(node)defquery(self,l:int,r:int,node:Node=None)->int:ifl>r:return0ifnodeisNone:node=self.rootifnode.l>=landnode.r<=r:returnnode.vself.pushdown(node)v=0ifl<=node.mid:v=max(v,self.query(l,r,node.left))ifr>node.mid:v=max(v,self.query(l,r,node.right))returnvdefpushup(self,node:Node):node.v=max(node.left.v,node.right.v)defpushdown(self,node:Node):ifnode.leftisNone:node.left=Node(node.l,node.mid)ifnode.rightisNone:node.right=Node(node.mid+1,node.r)ifnode.add:node.left.v+=node.addnode.right.v+=node.addnode.left.add+=node.addnode.right.add+=node.addnode.add=0classMyCalendarTwo:def__init__(self):self.tree=SegmentTree()defbook(self,startTime:int,endTime:int)->bool:ifself.tree.query(startTime+1,endTime)>=2:returnFalseself.tree.modify(startTime+1,endTime,1)returnTrue# Your MyCalendarTwo object will be instantiated and called as such:# obj = MyCalendarTwo()# param_1 = obj.book(startTime,endTime)
classNode{Nodeleft;Noderight;intl;intr;intmid;intv;intadd;publicNode(intl,intr){this.l=l;this.r=r;this.mid=(l+r)>>1;}}classSegmentTree{privateNoderoot=newNode(1,(int)1e9+1);publicSegmentTree(){}publicvoidmodify(intl,intr,intv){modify(l,r,v,root);}publicvoidmodify(intl,intr,intv,Nodenode){if(l>r){return;}if(node.l>=l&&node.r<=r){node.v+=v;node.add+=v;return;}pushdown(node);if(l<=node.mid){modify(l,r,v,node.left);}if(r>node.mid){modify(l,r,v,node.right);}pushup(node);}publicintquery(intl,intr){returnquery(l,r,root);}publicintquery(intl,intr,Nodenode){if(l>r){return0;}if(node.l>=l&&node.r<=r){returnnode.v;}pushdown(node);intv=0;if(l<=node.mid){v=Math.max(v,query(l,r,node.left));}if(r>node.mid){v=Math.max(v,query(l,r,node.right));}returnv;}publicvoidpushup(Nodenode){node.v=Math.max(node.left.v,node.right.v);}publicvoidpushdown(Nodenode){if(node.left==null){node.left=newNode(node.l,node.mid);}if(node.right==null){node.right=newNode(node.mid+1,node.r);}if(node.add!=0){Nodeleft=node.left,right=node.right;left.add+=node.add;right.add+=node.add;left.v+=node.add;right.v+=node.add;node.add=0;}}}classMyCalendarTwo{privateSegmentTreetree=newSegmentTree();publicMyCalendarTwo(){}publicbooleanbook(intstartTime,intendTime){if(tree.query(startTime+1,endTime)>=2){returnfalse;}tree.modify(startTime+1,endTime,1);returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo obj = new MyCalendarTwo(); * boolean param_1 = obj.book(startTime,endTime); */
classNode{public:intl,r,mid,v,add;Node*left;Node*right;Node(intl,intr):l(l),r(r),mid((l+r)>>1),v(0),add(0),left(nullptr),right(nullptr){}};classSegmentTree{public:Node*root;SegmentTree(){root=newNode(1,1e9+1);}voidmodify(intl,intr,intv,Node*node=nullptr){if(l>r){return;}if(node==nullptr){node=root;}if(node->l>=l&&node->r<=r){node->v+=v;node->add+=v;return;}pushdown(node);if(l<=node->mid){modify(l,r,v,node->left);}if(r>node->mid){modify(l,r,v,node->right);}pushup(node);}intquery(intl,intr,Node*node=nullptr){if(l>r){return0;}if(node==nullptr){node=root;}if(node->l>=l&&node->r<=r){returnnode->v;}pushdown(node);intv=0;if(l<=node->mid){v=max(v,query(l,r,node->left));}if(r>node->mid){v=max(v,query(l,r,node->right));}returnv;}private:voidpushup(Node*node){node->v=max(node->left->v,node->right->v);}voidpushdown(Node*node){if(node->left==nullptr){node->left=newNode(node->l,node->mid);}if(node->right==nullptr){node->right=newNode(node->mid+1,node->r);}if(node->add){node->left->v+=node->add;node->right->v+=node->add;node->left->add+=node->add;node->right->add+=node->add;node->add=0;}}};classMyCalendarTwo{public:SegmentTreetree;MyCalendarTwo(){}boolbook(intstartTime,intendTime){if(tree.query(startTime+1,endTime)>=2){returnfalse;}tree.modify(startTime+1,endTime,1);returntrue;}};/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(startTime,endTime); */
typenodestruct{left*noderight*nodel,mid,rintv,addint}funcnewNode(l,rint)*node{return&node{l:l,r:r,mid:int(uint(l+r)>>1),}}typesegmentTreestruct{root*node}funcnewSegmentTree()*segmentTree{return&segmentTree{root:newNode(1,1e9+1),}}func(t*segmentTree)modify(l,r,vint,n*node){ifl>r{return}ifn.l>=l&&n.r<=r{n.v+=vn.add+=vreturn}t.pushdown(n)ifl<=n.mid{t.modify(l,r,v,n.left)}ifr>n.mid{t.modify(l,r,v,n.right)}t.pushup(n)}func(t*segmentTree)query(l,rint,n*node)int{ifl>r{return0}ifn.l>=l&&n.r<=r{returnn.v}t.pushdown(n)v:=0ifl<=n.mid{v=max(v,t.query(l,r,n.left))}ifr>n.mid{v=max(v,t.query(l,r,n.right))}returnv}func(t*segmentTree)pushup(n*node){n.v=max(n.left.v,n.right.v)}func(t*segmentTree)pushdown(n*node){ifn.left==nil{n.left=newNode(n.l,n.mid)}ifn.right==nil{n.right=newNode(n.mid+1,n.r)}ifn.add!=0{n.left.add+=n.addn.right.add+=n.addn.left.v+=n.addn.right.v+=n.addn.add=0}}typeMyCalendarTwostruct{tree*segmentTree}funcConstructor()MyCalendarTwo{returnMyCalendarTwo{newSegmentTree()}}func(this*MyCalendarTwo)Book(startTimeint,endTimeint)bool{ifthis.tree.query(startTime+1,endTime,this.tree.root)>=2{returnfalse}this.tree.modify(startTime+1,endTime,1,this.tree.root)returntrue}/** * Your MyCalendarTwo object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Book(startTime,endTime); */
classNode{left:Node|null=null;right:Node|null=null;l:number;r:number;mid:number;v:number=0;add:number=0;constructor(l:number,r:number){this.l=l;this.r=r;this.mid=(l+r)>>1;}}classSegmentTree{privateroot:Node=newNode(1,1e9+1);modify(l:number,r:number,v:number,node:Node|null=this.root):void{if(l>r||!node){return;}if(node.l>=l&&node.r<=r){node.v+=v;node.add+=v;return;}this.pushdown(node);if(l<=node.mid){this.modify(l,r,v,node.left);}if(r>node.mid){this.modify(l,r,v,node.right);}this.pushup(node);}query(l:number,r:number,node:Node|null=this.root):number{if(l>r||!node){return0;}if(node.l>=l&&node.r<=r){returnnode.v;}this.pushdown(node);letv=0;if(l<=node.mid){v=Math.max(v,this.query(l,r,node.left));}if(r>node.mid){v=Math.max(v,this.query(l,r,node.right));}returnv;}privatepushup(node:Node):void{if(node.left&&node.right){node.v=Math.max(node.left.v,node.right.v);}}privatepushdown(node:Node):void{if(!node.left){node.left=newNode(node.l,node.mid);}if(!node.right){node.right=newNode(node.mid+1,node.r);}if(node.add){letleft=node.left;letright=node.right;left.add+=node.add;right.add+=node.add;left.v+=node.add;right.v+=node.add;node.add=0;}}}classMyCalendarTwo{privatetree:SegmentTree=newSegmentTree();constructor(){}book(startTime:number,endTime:number):boolean{if(this.tree.query(startTime+1,endTime)>=2){returnfalse;}this.tree.modify(startTime+1,endTime,1);returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */
classNode{constructor(l,r){this.left=null;this.right=null;this.l=l;this.r=r;this.mid=(l+r)>>1;this.v=0;this.add=0;}}classSegmentTree{constructor(){this.root=newNode(1,1e9+1);}modify(l,r,v,node=this.root){if(l>r||!node){return;}if(node.l>=l&&node.r<=r){node.v+=v;node.add+=v;return;}this.pushdown(node);if(l<=node.mid){this.modify(l,r,v,node.left);}if(r>node.mid){this.modify(l,r,v,node.right);}this.pushup(node);}query(l,r,node=this.root){if(l>r||!node){return0;}if(node.l>=l&&node.r<=r){returnnode.v;}this.pushdown(node);letv=0;if(l<=node.mid){v=Math.max(v,this.query(l,r,node.left));}if(r>node.mid){v=Math.max(v,this.query(l,r,node.right));}returnv;}pushup(node){if(node.left&&node.right){node.v=Math.max(node.left.v,node.right.v);}}pushdown(node){if(!node.left){node.left=newNode(node.l,node.mid);}if(!node.right){node.right=newNode(node.mid+1,node.r);}if(node.add){constleft=node.left;constright=node.right;left.add+=node.add;right.add+=node.add;left.v+=node.add;right.v+=node.add;node.add=0;}}}varMyCalendarTwo=function(){this.tree=newSegmentTree();};/** * @param {number} startTime * @param {number} endTime * @return {boolean} */MyCalendarTwo.prototype.book=function(startTime,endTime){if(this.tree.query(startTime+1,endTime)>=2){returnfalse;}this.tree.modify(startTime+1,endTime,1);returntrue;};/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */