My Calendar I LeetCode Solution

Last updated on July 19th, 2024 at 10:48 pm

Here, We see My Calendar I LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Dynamic Programming, Minimax

Companies

LinkedIn

Level of Question

Medium

My Calendar I LeetCode Solution

My Calendar I LeetCode Solution

Problem Statement

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.

double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).

The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.

Implement the MyCalendar class:

  • MyCalendar() Initializes the calendar object.
  • boolean book(int start, int end) Returns 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.

Example 1:
Input: [“MyCalendar”, “book”, “book”, “book”] [[], [10, 20], [15, 25], [20, 30]]
Output: [null, true, false, true]

Explanation:
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.

1. My Calendar I LeetCode Solution C++

class MyCalendar {
public:
    map&lt;int, int&gt; mp;
    MyCalendar() { 
    }
    bool book(int start, int end) {
        auto it = mp.upper_bound(start);
        if (it == mp.end() || it-&gt;second &gt;= end)
        {
            mp[end] = start;
            return true;
        }
        else
            return false;        
    }
};

2. My Calendar I LeetCode Solution Java

class MyCalendar {
    TreeMap&lt;Integer,Integer&gt; calendar = new TreeMap&lt;&gt;();
    public MyCalendar() {
        calendar.put(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }
    public boolean book(int start, int end) {
        Map.Entry&lt;Integer,Integer&gt; pair = calendar.higherEntry(start);
        boolean res = end &lt;= pair.getValue();
        if (res) calendar.put(end, start);
        return res;
    }
}

3. My Calendar I Solution JavaScript

var MyCalendar = function() {
  this.cal = []    
};
MyCalendar.prototype.book = function(start, end) {
    let l=0, r=this.cal.length-1
    while(l &lt;= r){
      const mid = Math.floor((r+l)/2)
      const [s,e] = this.cal[mid]
      if(s &lt; end &amp;&amp; start &lt; e ) return false
      if(start &gt;= e){
        l = mid+1
      }else{
        r = mid-1
      }
    }
  this.cal.splice(l, 0 ,[start, end])
  return true    
};

4. My Calendar I Solution Python

class MyCalendar(object):
    def __init__(self):
        self.calendar = []   
    def book(self, start, end):
        right = len(self.calendar)
        if right == 0:
            self.calendar.append((start, end))
            return True
        left = 0
        while left &lt; right:
            mid = int(left + (right - left)/2)
            if self.calendar[mid][1] &lt;= start:
                left = mid + 1
            else:
                right = mid
        if left == len(self.calendar):
            self.calendar.append((start, end))
            return True
        if self.calendar[left][0] &gt;= end:
            self.calendar.insert(left, (start, end))
            return True
        return False
Scroll to Top