Skip to content

729. My Calendar I

https://leetcode.com/problems/my-calendar-i/

js
var MyCalendar = function() {
  this.events = []
}

/**
 * @param {number} start
 * @param {number} end
 * @return {boolean}
 */
MyCalendar.prototype.book = function(start, end) {
  for (let i = 0; i < this.events.length; i++) {
    const [s, e] = this.events[i]
    if (!(start >= e || end <= s)) {
      return false
    }
  }

  this.events.push([start, end])
  return true
}

/**
 * Your MyCalendar object will be instantiated and called as such:
 * var obj = Object.create(MyCalendar).createNew()
 * var param_1 = obj.book(start,end)
 */
py
class MyCalendar:

    def __init__(self):
        self.events = []

    def book(self, start, end):
        """
        :type start: int
        :type end: int
        :rtype: bool
        """
        for event in self.events:
            s, e = event
            if not (start >= e or end <= s):
                return False

        self.events.append([start, end])
        return True


# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)