#3439
Reschedule Meetings for Maximum Free Time I
MediumArrayGreedySliding WindowSliding WindowGreedy Algorithms
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
The optimal approach uses a sliding window technique to efficiently calculate the maximum free time by focusing on the gaps between meetings. This allows us to maximize free time while only considering the necessary meetings.
⚙️
Algorithm
3 steps- 1Step 1: Calculate the gaps between consecutive meetings.
- 2Step 2: Use a sliding window of size k+1 to find the maximum sum of gaps that can be created by moving k meetings.
- 3Step 3: Return the maximum free time possible after rescheduling.
solution.py16 lines
1# Full working Python code
2
3def maxFreeTimeOptimal(eventTime, k, startTime, endTime):
4 n = len(startTime)
5 gaps = []
6 for i in range(n - 1):
7 gaps.append(startTime[i + 1] - endTime[i])
8 gaps.append(eventTime - endTime[-1]) # gap after last meeting
9 max_free_time = 0
10 current_sum = sum(gaps[:k + 1])
11 max_free_time = current_sum
12 for i in range(k + 1, len(gaps)):
13 current_sum += gaps[i] - gaps[i - (k + 1)]
14 max_free_time = max(max_free_time, current_sum)
15 return max_free_time
16ℹ
Complexity note: The complexity is O(n) because we only traverse the meetings and gaps a limited number of times, making it efficient for large inputs.
- 1Understanding how to manipulate gaps between meetings is crucial for maximizing free time.
- 2Using a sliding window technique allows for efficient calculation of maximum free time.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.