732. My Calendar III
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to implement a MyCalendarThree class that handles booking events. For each new event [startTime, endTime), we need to determine the maximum number of events that are simultaneously active at any point in time. This is referred to as the maximum k-booking.
Solution
My Calendar III in Python
class MyCalendarThree:
def __init__(self):
# A dictionary to store time points and their corresponding count changes.
# Python's dict doesn't maintain key-sorted order, so we'll sort keys manually.
self.timeline = {}
def book(self, startTime: int, endTime: int) -> int:
# Increment the count at the start time.
# +1 signifies a new event starting.
self.timeline[startTime] = self.timeline.get(startTime, 0) + 1
# Decrement the count at the end time.
# -1 signifies an event ending.
# The interval is [startTime, endTime), so it ends at 'endTime'.
self.timeline[endTime] = self.timeline.get(endTime, 0) - 1
active_events = 0
max_active_events = 0
# Get the time points and sort them to sweep through the timeline chronologically.
sorted_times = sorted(self.timeline.keys())
for time in sorted_times:
# Update the number of active events by the change at this time point.
active_events += self.timeline[time]
# Update the maximum number of concurrent events seen so far.
max_active_events = max(max_active_events, active_events)
return max_active_events
# Your MyCalendarThree object will be instantiated and called as such:
# obj = MyCalendarThree()
# param_1 = obj.book(startTime,endTime)
My Calendar III in Java
class MyCalendarThree {
// Use a TreeMap to store the change in active events at each time point.
// TreeMap automatically keeps the keys (time points) in sorted order.
private TreeMap<Integer, Integer> timeline;
public MyCalendarThree() {
timeline = new TreeMap<>();
}
public int book(int startTime, int endTime) {
// Increment the count at the start time.
// A value of +1 signifies a new event starting.
timeline.put(startTime, timeline.getOrDefault(startTime, 0) + 1);
// Decrement the count at the end time.
// A value of -1 signifies an event ending.
// The interval is [startTime, endTime), so it ends at 'endTime'.
timeline.put(endTime, timeline.getOrDefault(endTime, 0) - 1);
int activeEvents = 0;
int maxActiveEvents = 0;
// Iterate through the values of the TreeMap.
// TreeMap values are iterated in the order of their keys (time),
// so we are sweeping through the timeline.
for (int countDelta : timeline.values()) {
// Update the number of currently active events.
activeEvents += countDelta;
// Update the maximum number of concurrent events seen so far.
maxActiveEvents = Math.max(maxActiveEvents, activeEvents);
}
return maxActiveEvents;
}
}
/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree obj = new MyCalendarThree();
* int param_1 = obj.book(startTime,endTime);
*/
My Calendar III in C++
class MyCalendarThree {
private:
// Use a map to store the change in active events at each time point.
// std::map automatically keeps the keys (time points) in sorted order.
std::map<int, int> timeline;
public:
MyCalendarThree() {
// The map is default-initialized.
}
int book(int startTime, int endTime) {
// Increment the count at the start time.
// This signifies a new event beginning.
timeline[startTime]++;
// Decrement the count at the end time.
// This signifies an event ending.
// The interval is [startTime, endTime), so the event ends at 'endTime'.
timeline[endTime]--;
int active_events = 0;
int max_active_events = 0;
// Iterate through the timeline in chronological order.
// The std::map keeps keys sorted, so we can sweep through time.
for (auto const& [time, count_delta] : timeline) {
// Update the number of active events at this time point.
active_events += count_delta;
// Update the maximum number of concurrent events seen so far.
if (active_events > max_active_events) {
max_active_events = active_events;
}
}
return max_active_events;
}
};
/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree* obj = new MyCalendarThree();
* int param_1 = obj->book(startTime,endTime);
*/
My Calendar III in TypeScript
class MyCalendarThree {
// A Map to store time points and their corresponding count changes.
// JavaScript/TypeScript Map doesn't store keys in sorted order by value,
// so we'll need to sort the keys manually for the sweep-line scan.
private timeline: Map<number, number>;
constructor() {
this.timeline = new Map<number, number>();
}
book(startTime: number, endTime: number): number {
// Increment the count at the start time.
// +1 signifies a new event starting.
this.timeline.set(startTime, (this.timeline.get(startTime) || 0) + 1);
// Decrement the count at the end time.
// -1 signifies an event ending.
// The interval is [startTime, endTime), so it ends at 'endTime'.
this.timeline.set(endTime, (this.timeline.get(endTime) || 0) - 1);
let activeEvents = 0;
let maxActiveEvents = 0;
// Get the time points (keys) from the map and sort them numerically.
const sortedTimes = Array.from(this.timeline.keys()).sort((a, b) => a - b);
// Iterate through the sorted time points to perform the sweep-line scan.
for (const time of sortedTimes) {
// Update the number of active events by the change at this time point.
activeEvents += this.timeline.get(time)!;
// Update the maximum number of concurrent events seen so far.
maxActiveEvents = Math.max(maxActiveEvents, activeEvents);
}
return maxActiveEvents;
}
}
/**
* Your MyCalendarThree object will be instantiated and called as such:
* var obj = new MyCalendarThree()
* var param_1 = obj.book(startTime,endTime)
*/
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.