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 ...

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...

Dynamic Programming - Easy Level - Question 1

Dynamic Programming - Easy Level - Question 1 Leetcode 1646  Get Maximum in Generated Array You are given an integer n. An array nums of length n + 1 is generated in the following way: nums[0] = 0 nums[1] = 1 nums[2 * i] = nums[i] when 2 <= 2 * i <= n nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n Return the maximum integer in the array nums​​​. Constraints: 0 <= n <= 100 Analysis: This question is quick straightforward: the state and transitional formula are given; the initialization is also given. So we can just ready the code to iterate all the states and find the maximum. See the code below: class Solution { public: int getMaximumGenerated(int n) { int res = 0; if(n<2) return n; vector<int> f(n+1, 0); f[1] = 1; for(int i=2; i<=n; ++i) { if(i&1) f[i] = f[i/2] + f[i/2+1]; else f[i] = f[i/2]; // cout<<i<<" "<<f[i]<<endl; ...