LeetCode Solutions

729. My Calendar I

Time: $O(n^2)$

Space: $O(1)$

			

class MyCalendar {
 public:
  bool book(int start, int end) {
    for (const auto& [s, e] : timeline)
      if (max(start, s) < min(end, e))
        return false;
    timeline.emplace_back(start, end);
    return true;
  }

 private:
  vector<pair<int, int>> timeline;
};
			

class MyCalendar {
  public boolean book(int start, int end) {
    for (int[] t : timeline)
      if (Math.max(t[0], start) < Math.min(t[1], end))
        return false;
    timeline.add(new int[] {start, end});
    return true;
  }

  private List<int[]> timeline = new ArrayList<>();
}
			

class MyCalendar:
  def __init__(self):
    self.timeline = []

  def book(self, start: int, end: int) -> bool:
    for s, e in self.timeline:
      if max(start, s) < min(end, e):
        return False
    self.timeline.append((start, end))
    return True