Solving Leetcode Interviews in Seconds with AI: Reschedule Meetings for Maximum Free Time II
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3440" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given an integer eventTime denoting the duration of an event. You are also given two integer arrays startTime and endTime, each of length n. These represent the start and end times of n non-overlapping meetings that occur during the event between time t = 0 and time t = eventTime, where the ith meeting occurs during the time [startTime[i], endTime[i]]. You can reschedule at most one meeting by moving its start time while maintaining the same duration, such that the meetings remain non-overlapping, to maximize the longest continuous period of free time during the event. Return the maximum amount of free time possible after rearranging the meetings. Note that the meetings can not be rescheduled to a time outside the event and they should remain non-overlapping. Note: In this version, it is valid for the relative ordering of the meetings to change after rescheduling one meeting. Example 1: Input: eventTime = 5, startTime = [1,3], endTime = [2,5] Output: 2 Explanation: Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2]. Example 2: Input: eventTime = 10, startTime = [0,7,9], endTime = [1,8,10] Output: 7 Explanation: Reschedule the meeting at [0, 1] to [8, 9], leaving no meetings during the time [0, 7]. Example 3: Input: eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10] Output: 6 Explanation: Reschedule the meeting at [3, 4] to [8, 9], leaving no meetings during the time [1, 7]. Example 4: Input: eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5] Output: 0 Explanation: There is no time during the event not occupied by meetings. Constraints: 1 <= eventTime <= 109 n == startTime.length == endTime.length 2 <= n <= 105 0 <= startTime[i] < endTime[i] <= eventTime endTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].
Explanation
Here's a breakdown of the approach, followed by the Python code:
- Identify Free Slots: First, identify all the existing free time slots by sorting the meetings by their start times and then iterating through them to find gaps. Store these gaps.
- Iterate and Reschedule: For each meeting, determine its duration. Then iterate through all free time slots. Check if the meeting can be rescheduled to fit within a free time slot. If yes, calculate the new free time (considering the original free time slots plus the gap created by moving the meeting). Keep track of the maximum free time encountered.
Consider No Reschedule: Always consider the scenario where no meeting is rescheduled. This is done by calculating the initial maximum free time.
Runtime Complexity: O(n^2 log n), due to sorting the meetings, iterating through each meeting and all free time slots to check the possibilities of rescheduling.
- Storage Complexity: O(n), where n is the number of meetings to store meeting intervals and potentially free time intervals.
Code
def max_free_time(eventTime: int, startTime: list[int], endTime: list[int]) -> int:
"""
Calculates the maximum amount of free time possible after rescheduling at most one meeting.
Args:
eventTime: The duration of the event.
startTime: A list of start times of the meetings.
endTime: A list of end times of the meetings.
Returns:
The maximum amount of free time possible after rescheduling the meetings.
"""
n = len(startTime)
meetings = sorted(zip(startTime, endTime)) # Sort meetings based on start time
# Calculate initial free time slots
free_slots = []
if meetings[0][0] > 0:
free_slots.append((0, meetings[0][0]))
for i in range(n - 1):
free_slots.append((meetings[i][1], meetings[i+1][0]))
if meetings[-1][1] < eventTime:
free_slots.append((meetings[-1][1], eventTime))
# Calculate maximum initial free time
max_free = 0
for start, end in free_slots:
max_free = max(max_free, end - start)
# Try rescheduling each meeting
for i in range(n):
duration = meetings[i][1] - meetings[i][0]
#Try rescheduling the meeting to available free slots
for start, end in free_slots:
if duration <= end - start:
new_max_free = 0
temp_free_slots = []
#Adding existing free slots except the one we are using for rescheduling
for j in range(len(free_slots)):
if free_slots[j] != (start, end):
temp_free_slots.append(free_slots[j])
#Adding the free time created by removing the current meeting
temp_start_time = []
temp_end_time = []
for k in range(n):
if k != i:
temp_start_time.append(meetings[k][0])
temp_end_time.append(meetings[k][1])
temp_meetings = sorted(zip(temp_start_time, temp_end_time))
temp_free_slots_cal = []
if len(temp_meetings) > 0:
if temp_meetings[0][0] > 0:
temp_free_slots_cal.append((0, temp_meetings[0][0]))
for l in range(len(temp_meetings) - 1):
temp_free_slots_cal.append((temp_meetings[l][1], temp_meetings[l+1][0]))
if temp_meetings[-1][1] < eventTime:
temp_free_slots_cal.append((temp_meetings[-1][1], eventTime))
else:
temp_free_slots_cal = [(0, eventTime)]
#Merge the free slots from the available free slot & other slots
for s,e in temp_free_slots_cal:
temp_free_slots.append((s,e))
for free_start, free_end in temp_free_slots:
new_max_free = max(new_max_free, free_end - free_start)
max_free = max(max_free, new_max_free)
return max_free