2Brute2Best
πŸ”₯0

My Calendar I

MediumΒ·LeetCode #729 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

Build a calendar class that never allows a double booking: two events that share some moment of time.

  • MyCalendar() creates an empty calendar.
  • bool book(int startTime, int endTime) tries to add an event covering the half-open range [startTime, endTime), meaning every time t with startTime ≀ t < endTime. If it does not overlap any event already in the calendar, add it and return true. Otherwise do not add it and return false.

Because the end is not included, an event ending at 20 and one starting at 20 do not overlap.

Input

A sequence of book(startTime, endTime) calls.

Output

For each call, true if the event was booked, false if it was rejected.

Example 1
Input: book(10, 20), book(15, 25), book(20, 30)
Output: true, false, true

[10,20) is booked. [15,25) overlaps it at 15 … 19, so it is rejected. [20,30) starts exactly when [10,20) ends, so it fits.

Constraints
  • β€’ 0 ≀ startTime < endTime ≀ 10^9
  • β€’ At most 1000 calls to book
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œbook(start, end) returns true/false”
β†’ Online version of Meeting Rooms: events arrive one at a time, so we can't sort them all up front.
β€œno double booking”
β†’ At most 1 event at any moment β†’ only the neighbours of the new event can clash.
β€œhalf-open [start, end)”
β†’ Touching ends are fine; the overlap test uses strict `<` on both sides.
03

Approach Complexity Ladder

Brute Force β€” check every bookingO(N)O(N)

Keep a list of bookings; scan all of them on every call

Optimal β€” sorted map, check two neighboursO(log N)O(N)

map start β†’ end; only the booking at lower_bound(start) and the one before it can clash

Brute Force Intuition

Store every accepted booking in a vector. For a new event [s, e), loop over all bookings [s2, e2) and reject if s < e2 && s2 < e. If none clash, push it into the vector.

Recognition rule for this sub-pattern:

How many things are active at the same time? Sweep events in time order, or keep a min-heap of end times.
Remember this Β· Intervals
Trigger
Input is [start, end] pairs; overlap, merge, insert, meeting rooms, free time, minimum removals.
Think
After sorting, do I only ever need to compare against the most recent interval?
Key question
Sort by start or by end β€” which one makes the greedy choice obvious?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.