Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 2402: Meeting Rooms III

LeetCode 2402 Solution 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)

LeetCode 2402 Solutions in Java, C++, Python

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);
    }
}

Interactive Code Editor for LeetCode 2402

Improve Your LeetCode 2402 Solution

Use the editor below to refine the provided solution for LeetCode 2402. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems