Leetcode 729: My Calendar I

grid47
grid47
Exploring patterns and algorithms
Aug 26, 2024 5 min read

A calendar where events are added, with each event softly glowing as it is scheduled.
Solution to LeetCode 729: My Calendar I Problem

You are implementing a calendar application where you can add events. Each event has a start time and end time represented by a half-open interval [start, end). An event can only be added if it does not overlap with existing events in the calendar. Return true if the event can be booked successfully, and false if it causes a conflict.
Problem
Approach
Steps
Complexity
Input: The input consists of an array of method calls with parameters. The first call is always initializing the MyCalendar object, followed by calls to the book method.
Example: Input: ["MyCalendar", "book", "book", "book"] [[], [10, 20], [15, 25], [20, 30]]
Constraints:
• 0 <= start < end <= 10^9
• At most 1000 calls will be made to book.
Output: The output will be an array of results for each call. For the MyCalendar initialization, the result will be null, and for each book method call, the result will be a boolean indicating whether the event was successfully booked or not.
Example: Output: [null, true, false, true]
Constraints:
• For each call to book, return true if the event can be booked, false otherwise.
Goal: The goal is to implement a calendar that checks for overlapping events and returns whether a new event can be added without causing a conflict.
Steps:
• 1. Create a map or structure to store the events, with start times as the key.
• 2. For each new event, check if it overlaps with any existing event.
• 3. If no overlap is found, add the event to the calendar and return true.
• 4. If overlap is found, return false and do not add the event.
Goal: The constraints define the input limits.
Steps:
• 0 <= start < end <= 10^9
• 1 <= k <= 1000
Assumptions:
• The input start and end times are integers, and the events are well-formed.
Input: Input: ["MyCalendar", "book", "book", "book"] [[], [10, 20], [15, 25], [20, 30]]
Explanation: In the first example, the first event is successfully booked as no previous events exist. The second event cannot be booked as it overlaps with the first event. The third event is successfully booked since it does not overlap with the first event (ends at 20).

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus