LeetCode 2402: Meeting Rooms III Solution

Master LeetCode problem 2402 (Meeting Rooms III), a hard 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.

Problem Explanation

Explanation

To solve this problem, we can use a priority queue to keep track of the end times of the meetings in the rooms. We iterate through the meetings sorted by start time, and for each meeting, we check if there are any available rooms. If there are, we assign the meeting to the room with the earliest end time. If there are no available rooms, we delay the meeting by adding it to the priority queue.

We maintain a count of meetings held in each room and return the room with the maximum count.

Time complexity: O(n log n) where n is the number of meetings Space complexity: O(n)

Solution Code

import java.util.*;

class Solution {
    public int meetingRoomIII(int n, int[][] meetings) {
        Arrays.sort(meetings, (a, b) -> a[0] - b[0]);
        PriorityQueue<int[]> rooms = new PriorityQueue<>((a, b) -> a[1] - b[1]);
        int[] count = new int[n];
        
        int maxMeetings = 0;
        for (int[] meeting : meetings) {
            while (!rooms.isEmpty() && rooms.peek()[1] <= meeting[0]) {
                int[] room = rooms.poll();
                count[room[0]]++;
                maxMeetings = Math.max(maxMeetings, count[room[0]]);
            }
            
            int room = rooms.size();
            rooms.offer(new int[]{room, meeting[1]});
        }
        
        return Arrays.binarySearch(count, maxMeetings);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2402 (Meeting Rooms III)?

This page provides optimized solutions for LeetCode problem 2402 (Meeting Rooms III) 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 2402 (Meeting Rooms III)?

The time complexity for LeetCode 2402 (Meeting Rooms III) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2402 on DevExCode?

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

Back to LeetCode Solutions