LeetCode 3439: Reschedule Meetings for Maximum Free Time I Solution

Master LeetCode problem 3439 (Reschedule Meetings for Maximum Free Time I), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3439. Reschedule Meetings for Maximum Free Time I

Problem Explanation

Explanation

To maximize the longest continuous period of free time during the event, we need to find the maximum free time by rescheduling at most k meetings. We can achieve this by sorting the meetings based on their end times and then iterating through the meetings while keeping track of the maximum free time. We maintain a priority queue to track the end times of the meetings.

  1. Sort the meetings based on end times.
  2. Initialize a priority queue to store the end times of the meetings.
  3. Iterate through the sorted meetings:
    • If the priority queue is not empty and we can reschedule a meeting (k > 0):
      • Pop the meeting with the earliest end time from the priority queue.
      • Calculate the free time between the current meeting's start time and the popped meeting's end time.
      • Update the maximum free time if needed.
      • Decrement k.
    • Push the current meeting's end time into the priority queue.
  4. Return the maximum free time.

Time Complexity: O(nlogn) where n is the number of meetings. Sorting the meetings based on end times takes O(nlogn) time.

Space Complexity: O(n) for the priority queue to store end times.

Solution Code

import java.util.Arrays;
import java.util.PriorityQueue;

class Solution {
    public int maxFreeTime(int eventTime, int k, int[] startTime, int[] endTime) {
        int n = startTime.length;
        int maxFreeTime = 0;
        
        if (n == 0) return eventTime;
        
        int[][] meetings = new int[n][2];
        for (int i = 0; i < n; i++) {
            meetings[i][0] = startTime[i];
            meetings[i][1] = endTime[i];
        }
        
        Arrays.sort(meetings, (a, b) -> a[1] - b[1]);
        
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        for (int i = 0; i < n; i++) {
            if (!pq.isEmpty() && k > 0) {
                int prevEnd = pq.poll();
                int freeTime = meetings[i][0] - prevEnd;
                maxFreeTime = Math.max(maxFreeTime, freeTime);
                k--;
            }
            pq.offer(meetings[i][1]);
        }
        
        return maxFreeTime;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3439 (Reschedule Meetings for Maximum Free Time I)?

This page provides optimized solutions for LeetCode problem 3439 (Reschedule Meetings for Maximum Free Time I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3439 (Reschedule Meetings for Maximum Free Time I)?

The time complexity for LeetCode 3439 (Reschedule Meetings for Maximum Free Time I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3439 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3439 in Java, C++, or Python.

Back to LeetCode Solutions