My Calendar III LeetCode Solution

Here, We see My Calendar III 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

My Calendar III LeetCode Solution

My Calendar III LeetCode Solution

Problem Statement

k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events.)

You are given some events [startTime, endTime), after each given event, return an integer k representing the maximum k-booking between all the previous events.

Implement the MyCalendarThree class:

  • MyCalendarThree() Initializes the object.
  • int book(int startTime, int endTime) Returns an integer k representing the largest integer such that there exists a k-booking in the calendar.

Example 1:
Input [“MyCalendarThree”, “book”, “book”, “book”, “book”, “book”, “book”] [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output [null, 1, 1, 2, 3, 3, 3]
Explanation MyCalendarThree myCalendarThree = new MyCalendarThree(); myCalendarThree.book(10, 20); // return 1 myCalendarThree.book(50, 60); // return 1 myCalendarThree.book(10, 40); // return 2 myCalendarThree.book(5, 15); // return 3 myCalendarThree.book(5, 10); // return 3 myCalendarThree.book(25, 55); // return 3

My Calendar III LeetCode Solution C++

class MyCalendarThree {
public:
    MyCalendarThree() {}
    
    int book(int start, int end) {
        lines[start]++;
        lines[end]--;
        int mx = 0, cnt = 0;
        for (auto x : lines) {
            cnt += x.second;
            mx = max(mx, cnt);
        }
        return mx;
    }
private:
    map<int, int> lines;
};Code language: PHP (php)

My Calendar III LeetCode Solution Java

class MyCalendarThree {
    private TreeMap<Integer, Integer> lines;
   
    public MyCalendarThree() {
        lines = new TreeMap<>();
    }
    
    public int book(int start, int end) {
        lines.put(start, lines.getOrDefault(start, 0) + 1);
        lines.put(end, lines.getOrDefault(end, 0) - 1);
        int mx = 0, cnt = 0;
        for (int x : lines.values()) {
            cnt += x;
            mx = Math.max(mx, cnt);
        }
        return mx;
    }    
}Code language: PHP (php)

My Calendar III LeetCode Solution JavaScript

var MyCalendarThree = function() {
    this.tm = {}
};

MyCalendarThree.prototype.book = function(start, end) {
    this.tm[start] = (this.tm[start] || 0) + 1
    this.tm[end] = (this.tm[end] || 0) - 1
    let max = count = 0
    for(let val in this.tm){
        max = Math.max(max, count += this.tm[val])
    }
    return max
};Code language: JavaScript (javascript)

My Calendar III Solution Python

class MyCalendarThree(object):

    def __init__(self):
        self.maxOverlaps = 1
        self.bookings = []

    def checkOverlaps_boundryCount(self):
        lst = []
        for start, end in self.bookings:
            lst.append((start, +1))
            lst.append((end, -1))
        lst.sort()
        
        overlaps = 0
        for b in lst:
            overlaps += b[1]
            self.maxOverlaps = max(self.maxOverlaps, overlaps)
        return self.maxOverlaps
		
    def book(self, start, end):
       self.bookings.append((start, end))
       return self.checkOverlaps_boundryCount()
Scroll to Top