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

Sweep Line

Sweep (or scanning) line algorithm is very efficient for some specific questions involving discrete intervals. The intervals could be the lasting time of events, or the width of a building or an abstract square, etc. In the scanning line algorithm, we usually need to distinguish the start and the end of an interval. After the labeling of the starts and ends, we can sort them together based on the values of the starts and ends. Thus, if there are N intervals in total, we will have 2*N data points (since each interval will contribute 2). The sorting becomes the most time-consuming step, which is O(2N*log(2N) ~ O(N*logN). After the sorting, we usually can run a linear sweep for all the data points. If the data point is labeled as a starting point, it means a new interval is in the processing; when an ending time is reached, it means one of the interval has ended. In such direct way, we can easily figure out how many intervals are in the processes. Other related information can also be obt...

Bit Manipulation - Example

  Leetcode 136 Single Number Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Constraints: 1 <= nums.length <= 3 * 10^4 -3 * 10^4 <= nums[i] <= 3 * 10^4 Each element in the array appears twice except for one element which appears only once. Analysis: If there is no space limitation, this question can be solved by counting easily. But counting requires additional space. Here we can use xor (^) operation based on some interesting observations:  A^A = 0, here A is any number A^0 = A, here A is any number Since all the number appears twice except one, then all the number appear even numbers will be cancelled out, and only the number appears one time is left, which is what we want. See the code below: class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for(auto ...

Algorithm Advance Outline

Who wants to read these notes? 1. the one who wants to learn algorithm; 2. the one who has a planned interview in a short amount of time, such as one month later; 3. purely out of curiosity; 4. all the rest. The primary purpose for these posts is to help anyone who wants to learn algorithm in a short amount of time and be ready for coding interviews with tech companies, such as, Amazon, Facebook, Microsoft, etc. Before you start, you are expected to already know:  1. the fundamentals of at least one programming language; 2. the fundamentals of data structures. If you do not have these basics, it is better to "google and read" some intro docs, which should NOT take large amount of time. Of course, you can always learn whenever see a "unknown" data structure or line in the codes. Remember, "google and read" is always one of keys to learning. Common algorithms: 1. Recursion 2. Binary search 3. Dynamic programming 4. Breadth-first search 5. Depth-first search...