返回介绍

solution / 2400-2499 / 2406.Divide Intervals Into Minimum Number of Groups / README_EN

发布于 2024-06-17 01:03:06 字数 4906 浏览 0 评论 0 收藏 0

2406. Divide Intervals Into Minimum Number of Groups

中文文档

Description

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

Solutions

Solution 1: Greedy + Priority Queue (Min Heap)

First, we sort the intervals by their left endpoints. We use a min heap to maintain the rightmost endpoint of each group (the top of the heap is the minimum of the rightmost endpoints of all groups).

Next, we traverse each interval:

  • If the left endpoint of the current interval is greater than the top element of the heap, it means the current interval can be added to the group where the top element of the heap is located. We directly pop the top element of the heap, and then put the right endpoint of the current interval into the heap.
  • Otherwise, it means there is currently no group that can accommodate the current interval, so we create a new group and put the right endpoint of the current interval into the heap.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array intervals.

class Solution:
  def minGroups(self, intervals: List[List[int]]) -> int:
    h = []
    for a, b in sorted(intervals):
      if h and h[0] < a:
        heappop(h)
      heappush(h, b)
    return len(h)
class Solution {
  public int minGroups(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
    PriorityQueue<Integer> q = new PriorityQueue<>();
    for (var e : intervals) {
      if (!q.isEmpty() && q.peek() < e[0]) {
        q.poll();
      }
      q.offer(e[1]);
    }
    return q.size();
  }
}
class Solution {
public:
  int minGroups(vector<vector<int>>& intervals) {
    sort(intervals.begin(), intervals.end());
    priority_queue<int, vector<int>, greater<int>> q;
    for (auto& e : intervals) {
      if (q.size() && q.top() < e[0]) {
        q.pop();
      }
      q.push(e[1]);
    }
    return q.size();
  }
};
func minGroups(intervals [][]int) int {
  sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })
  q := hp{}
  for _, e := range intervals {
    if q.Len() > 0 && q.IntSlice[0] < e[0] {
      heap.Pop(&q)
    }
    heap.Push(&q, e[1])
  }
  return q.Len()
}

type hp struct{ sort.IntSlice }

func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
  a := h.IntSlice
  v := a[len(a)-1]
  h.IntSlice = a[:len(a)-1]
  return v
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文