LeetCode 3440: Reschedule Meetings for Maximum Free Time II Solution

Master LeetCode problem 3440 (Reschedule Meetings for Maximum Free Time II), 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.

3440. Reschedule Meetings for Maximum Free Time II

Problem Explanation

Explanation

To maximize the longest continuous period of free time during the event, we need to find the meeting that can be rescheduled to create the maximum free time. We can achieve this by iterating through the meetings, calculating the free time between each pair of meetings. We then find the meeting that, when rescheduled, creates the maximum free time.

  1. Sort the meetings based on their start times.
  2. Iterate through the meetings to calculate the free time between each pair of meetings.
  3. Find the meeting that, when rescheduled, creates the maximum free time.

Time complexity: O(n log n) - Sorting the meetings Space complexity: O(n) - Space required to store the meetings

Solution Code

import java.util.*;

class Solution {
    public int maxFreeTime(int eventTime, int[] startTime, int[] endTime) {
        int n = startTime.length;
        List<int[]> meetings = new ArrayList<>();
        
        for (int i = 0; i < n; i++) {
            meetings.add(new int[]{startTime[i], endTime[i]});
        }
        
        Collections.sort(meetings, (a, b) -> a[0] - b[0]);
        
        int maxFreeTime = 0;
        for (int i = 1; i < n; i++) {
            maxFreeTime = Math.max(maxFreeTime, meetings.get(i)[0] - meetings.get(i-1)[1]);
        }
        
        return maxFreeTime;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3440 (Reschedule Meetings for Maximum Free Time II)?

This page provides optimized solutions for LeetCode problem 3440 (Reschedule Meetings for Maximum Free Time II) 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 3440 (Reschedule Meetings for Maximum Free Time II)?

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

Can I run code for LeetCode 3440 on DevExCode?

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

Back to LeetCode Solutions