Skip to main content

Sweep Line - Question 1

Question 1: The minimum meeting room needed

Say we have had a list of meetings with start_time and end_time. For each meeting room, only one meeting can be hold once the meeting starts, and it cannot hold the next meeting until the first one ends. For example, if the first meeting start at 1 and end at 10, then the meeting room can only hold a second meeting starting at or later than 10.

The question is to find the minimum number of meeting rooms needed to hold all the meetings.

Example 1:

meetings = [[2, 5], [4, 6], [5, 9]]

Ans: 2

the first meeting room can hold: [2, 5], [5, 9];

the second meeting room can hold: [4, 6].

Example 2:

meetings = [[1, 10], [2, 3], [3, 7], [4, 6], [5, 8], [6, 9], [7, 11], [10, 12]]

Ans: 4

the first meeting room can hold: [1, 10], [10, 12];

the second meeting room can hold: [2, 3], [3, 7], [7, 11];

the third meeting room can hold: [4, 6], [6, 9];

the fourth meeting room can hold: [5, 8].


Analysis:

As indicated by the second example, we can sort the meeting time based on the starting time. Once we choose the first meeting, then we can search for the next meeting that can be held in the same meeting room. The starting time of the next meeting should be equal to or larger than the ending time of the first meeting.

In this way, we can find a set of meetings that can be held in one meeting room.

But there are other meetings which cannot be held in the same meeting room.

So we need to search the list again, and just repeat the process for the first meeting room, to find all the meetings that can be held in the second meeting room.

To avoid redundancy, we can use a labeling vector to label whether the meeting has been held or not. Or to make sure each meeting be held once.

The time complexity is O(N^2). The initial sorting is O(N^log(N)) which is not the most expensive step.

Is there a faster way?

The answer is yes. For example, using the scanning line algorithm.

The first step is to label the starting and ending times of all meetings. The start time can be 1, the end time can be -1. Then sort all the times increasingly. If the start time equals to end time, then put the end time first.

After this step, we just need to scan through the sorted time list once, to calculate the maximum number of meeting that have overlaps in time. This value is exactly the minimum number of meeting room needed to hold all the meetings on the list.


See the code below:

int minMR(vector<vector<int>>& rs) {
    int res = 0, ct = 0;
    vector<vector<int>> data;
    for(auto &a : rs) {
	data.push_back({a[0], 1});
	data.push_back({a[1], -1});
    }
    sort(data.begin(), data.end(), [](vector<int> &a, vector<int> &b) {
	if(a[0] == b[0]) return a[1] < b[1];
	return a[0] < b[0];
    });
    for(auto &a : data) {
	if(a[1] == 1) ++ct;
	else --ct;
	res = max(res, ct);
    }
    return res;
}



Upper Layer

Comments

Popular posts from this blog

Brute Force - Question 2

2105. Watering Plants II Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way: Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously. It takes the same amount of time to water each plant regardless of how much water it needs. Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant. In case both Alice and Bob reach the same plant, the one with more water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice ...

Sliding Window - Question 1

Leetcode 76. Minimum Window Substring Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "". The testcases will be generated such that the answer is unique. A substring is a contiguous sequence of characters within the string. Constraints: m == s.length n == t.length 1 <= m, n <= 10^5 s and t consist of uppercase and lowercase English letters. Analysis: The first step to do is to count the letters in string t, since the relative order of chars does not matter. After the counting, we can use two pointers method on string s: starting with beginning of the string s, the second pointer can continue to move until the sub-string [i, j) has all the chars in t, where i the index of the first pointer, and j is the index of the second pointer. We are looking for the sub-string with the minimum leng...