Skip to content

Latest commit

 

History

History

2406

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents the inclusive interval [lefti, righti].

You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other.

Return the minimum number of groups you need to make.

Two intervals intersect if there is at least one common number between them. For example, the intervals [1, 5] and [5, 8] intersect.

 

Example 1:

Input: intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]
Output: 3
Explanation: We can divide the intervals into the following groups:
- Group 1: [1, 5], [6, 8].
- Group 2: [2, 3], [5, 10].
- Group 3: [1, 10].
It can be proven that it is not possible to divide the intervals into fewer than 3 groups.

Example 2:

Input: intervals = [[1,3],[5,6],[8,10],[11,13]]
Output: 1
Explanation: None of the intervals overlap, so we can put all of them in one group.

 

Constraints:

  • 1 <= intervals.length <= 105
  • intervals[i].length == 2
  • 1 <= lefti <= righti <= 106

Companies: Amazon, Adobe, Walmart Labs

Related Topics:
Array, Two Pointers, Greedy, Sorting, Heap (Priority Queue), Prefix Sum

Similar Questions:

Hints:

  • Can you find a different way to describe the question?
  • The minimum number of groups we need is equivalent to the maximum number of intervals that overlap at some point. How can you find that?

Solution 1. Greedy

// OJ: https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    int minGroups(vector<vector<int>>& A) {
        multiset<vector<int>> s(begin(A), end(A)); // Sort intervals in ascending order of start time
        int ans = 0;
        while (s.size()) {
            auto it = s.begin();
            while (it != s.end()) {
                int R = (*it)[1];
                s.erase(it);
                it = s.lower_bound({R+1,0}); // Greedily pick the earliest interval whose start time is after the previously picked interval's end time
            }
            ++ans;
        }
        return ans;
    }
};

Solution 2. Min Heap

// OJ: https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    int minGroups(vector<vector<int>>& A) {
        sort(begin(A), end(A)); // sort intervals in ascending order of start time
        priority_queue<int, vector<int>, greater<>> pq; // min heap of end times of active intervals
        int ans = 0;
        for (auto &v : A) {
            int left = v[0], right = v[1];
            while (pq.size() && pq.top() < left) pq.pop(); // deactivate all the intervals ends before the current one
            pq.push(right); // activate the current interval
            ans = max(ans, (int)pq.size()); // Try updating answer with the number of active intervals.
        }
        return ans;
    }
};