Swift Coding Test Course, Assigning Meeting Rooms

Problem Description

The meeting room management system is a process that allocates meeting rooms according to the schedule of meetings to use multiple meeting rooms efficiently. Given the start and end times of a meeting, propose a method to allocate meeting rooms as efficiently as possible.

When a specific meeting starts, it is necessary to find a meeting room that is not occupied by another meeting. The meeting room allocation problem includes the following inputs:

  • The number of meetings n
  • An array meetings containing the start and end times of the meetings, where each meeting consists of a start time and an end time.

Input

n = 3
meetings = [(0, 30), (5, 10), (15, 20)]

Output

Number of meeting rooms: 2

Problem Solving Approach

To solve this problem, I will follow these steps:

  1. Sort the meetings based on their end times with respect to their start and end times.
  2. Iterate through each meeting, comparing the start time of the current meeting with the end time of the previous meeting.
  3. If a meeting room is needed, increase the number of meeting rooms, and release that room when the meeting ends.

Algorithm Implementation

I will implement an algorithm to solve the meeting room allocation problem using Swift. Below is the actual implementation code:

func minMeetingRooms(_ meetings: [[Int]]) -> Int {
    guard meetings.count > 0 else {
        return 0
    }
    
    var startTimes = meetings.map { $0[0] }
    var endTimes = meetings.map { $0[1] }
    
    startTimes.sort()
    endTimes.sort()
    
    var roomCount = 0
    var endPointer = 0
    
    for startTime in startTimes {
        if startTime < endTimes[endPointer] {
            roomCount += 1
        } else {
            endPointer += 1
        }
    }
    
    return roomCount
}

Code Explanation

The above code separates the start and end times of each meeting from the input array and sorts them. Then it uses two pointers to compare the start and end times of the meetings to calculate the number of meeting rooms needed.

  1. The guard statement returns 0 if there are no meetings.
  2. Extract and sort the start and end times of the meetings.
  3. The first pointer checks the start time while iterating through each meeting, and the second pointer tracks the end times.
  4. If the start time is earlier than the end time, a new meeting room is needed, so roomCount is increased.
  5. After all meetings are processed, the number of required meeting rooms is returned.

Complexity Analysis

This algorithm has the following complexities:

  • Time Complexity: O(n log n) – it takes O(n log n) time to sort the start and end times.
  • Space Complexity: O(n) – it uses additional space to store the meeting start and end times.

Conclusion

The meeting room allocation problem is an important issue of managing overlapping meetings. The given algorithm can enhance the efficiency of meeting room usage and is a common problem in coding tests. Understanding how to solve the problem with Swift and practicing actual code implementations can be beneficial.

Additional Practice Problems

  • Determine how to handle cases where there are meetings with the same end time.
  • Randomly generate meeting times to test meeting room allocation based on the given number of meetings.

I hope this post helps you. I encourage you to improve your algorithm problem-solving skills through this problem!