Saturday, February 20, 2021

[Google Question][LeetCode] My Calendar I

Problem: Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.

Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.

A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.

Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

Example:

MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
Explanation: 
The first event can be booked.  The second can't because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.


Approach: As the intervals are coming one by one, we need to store them in balanced binary search tree to maintain the sorting order for faster check and storage.


Implementation in C#:

    public class MyCalendar

    {

        public MyCalendar()

        {

            this.intervalTree = new IntervalTree();

        }


        public bool Book(int start, int end)

        {

            if (start >= end)

            {

                return false;

            }

            return this.intervalTree.InsertInterval(start, end);

        }


        private IntervalTree intervalTree;

    }


    public class IntervalTreeNode

    {

        public int Start { get; private set; }

        public int End { get; private set; }

        public IntervalTreeNode Left { get; set; }

        public IntervalTreeNode Right { get; set; }


        public IntervalTreeNode(int start, int end, IntervalTreeNode left = null, IntervalTreeNode right = null)

        {

            this.Start = start;

            this.End = end;

            this.Left = left;

            this.Right = null;

        }

    }


    public class IntervalTree

    {

        public IntervalTree()

        {

            this.root = null;

        }


        public bool InsertInterval(int start, int end)

        {

            if (this.root == null)

            {

                this.root = new IntervalTreeNode(start, end);

                return true;

            }

            return this.InsertInterval(root, start, end);

        }


        private bool InsertInterval(IntervalTreeNode node, int start, int end)

        {

            if (node.End <= start)

            {

                if (node.Left == null)

                {

                    node.Left = new IntervalTreeNode(start, end);

                }

                else

                {

                    return this.InsertInterval(node.Left, start, end);

                }

            }

            else if (node.Start >= end)

            {

                if (node.Right == null)

                {

                    node.Right = new IntervalTreeNode(start, end);

                }

                else

                {

                    return this.InsertInterval(node.Right, start, end);

                }

            }

            else

            {

                return false;

            }

            return true;

        }


        private IntervalTreeNode root;

    }


Complexity: O(nlogn)

No comments:

Post a Comment