返回介绍

solution / 2500-2599 / 2580.Count Ways to Group Overlapping Ranges / README_EN

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

2580. Count Ways to Group Overlapping Ranges

中文文档

Description

You are given a 2D integer array ranges where ranges[i] = [starti, endi] denotes that all integers between starti and endi (both inclusive) are contained in the ith range.

You are to split ranges into two (possibly empty) groups such that:

  • Each range belongs to exactly one group.
  • Any two overlapping ranges must belong to the same group.

Two ranges are said to be overlapping if there exists at least one integer that is present in both ranges.

  • For example, [1, 3] and [2, 5] are overlapping because 2 and 3 occur in both ranges.

Return _the total number of ways to split_ ranges _into two groups_. Since the answer may be very large, return it modulo 109 + 7.

 

Example 1:

Input: ranges = [[6,10],[5,15]]
Output: 2
Explanation: 
The two ranges are overlapping, so they must be in the same group.
Thus, there are two possible ways:
- Put both the ranges together in group 1.
- Put both the ranges together in group 2.

Example 2:

Input: ranges = [[1,3],[10,20],[2,5],[4,8]]
Output: 4
Explanation: 
Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group.
Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group. 
Thus, there are four possible ways to group them:
- All the ranges in group 1.
- All the ranges in group 2.
- Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2.
- Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1.

 

Constraints:

  • 1 <= ranges.length <= 105
  • ranges[i].length == 2
  • 0 <= starti <= endi <= 109

Solutions

Solution 1: Sorting + Counting + Fast Power

We can first sort the intervals in the range, merge the overlapping intervals, and count the number of non-overlapping intervals, denoted as $cnt$.

Each non-overlapping interval can be chosen to be put in the first group or the second group, so the number of plans is $2^{cnt}$. Note that $2^{cnt}$ may be very large, so we need to take modulo $10^9 + 7$. Here, we can use fast power to solve this problem.

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

Alternatively, we can also avoid using fast power. Once a new non-overlapping interval is found, we multiply the number of plans by 2 and take modulo $10^9 + 7$.

class Solution:
  def countWays(self, ranges: List[List[int]]) -> int:
    ranges.sort()
    cnt, mx = 0, -1
    for start, end in ranges:
      if start > mx:
        cnt += 1
      mx = max(mx, end)
    mod = 10**9 + 7
    return pow(2, cnt, mod)
class Solution {
  public int countWays(int[][] ranges) {
    Arrays.sort(ranges, (a, b) -> a[0] - b[0]);
    int cnt = 0, mx = -1;
    for (int[] e : ranges) {
      if (e[0] > mx) {
        ++cnt;
      }
      mx = Math.max(mx, e[1]);
    }
    return qpow(2, cnt, (int) 1e9 + 7);
  }

  private int qpow(long a, int n, int mod) {
    long ans = 1;
    for (; n > 0; n >>= 1) {
      if ((n & 1) == 1) {
        ans = ans * a % mod;
      }
      a = a * a % mod;
    }
    return (int) ans;
  }
}
class Solution {
public:
  int countWays(vector<vector<int>>& ranges) {
    sort(ranges.begin(), ranges.end());
    int cnt = 0, mx = -1;
    for (auto& e : ranges) {
      cnt += e[0] > mx;
      mx = max(mx, e[1]);
    }
    using ll = long long;
    auto qpow = [&](ll a, int n, int mod) {
      ll ans = 1;
      for (; n; n >>= 1) {
        if (n & 1) {
          ans = ans * a % mod;
        }
        a = a * a % mod;
      }
      return ans;
    };
    return qpow(2, cnt, 1e9 + 7);
  }
};
func countWays(ranges [][]int) int {
  sort.Slice(ranges, func(i, j int) bool { return ranges[i][0] < ranges[j][0] })
  cnt, mx := 0, -1
  for _, e := range ranges {
    if e[0] > mx {
      cnt++
    }
    if mx < e[1] {
      mx = e[1]
    }
  }
  qpow := func(a, n, mod int) int {
    ans := 1
    for ; n > 0; n >>= 1 {
      if n&1 == 1 {
        ans = ans * a % mod
      }
      a = a * a % mod
    }
    return ans
  }
  return qpow(2, cnt, 1e9+7)
}
function countWays(ranges: number[][]): number {
  ranges.sort((a, b) => a[0] - b[0]);
  let mx = -1;
  let ans = 1;
  const mod = 10 ** 9 + 7;
  for (const [start, end] of ranges) {
    if (start > mx) {
      ans = (ans * 2) % mod;
    }
    mx = Math.max(mx, end);
  }
  return ans;
}

Solution 2

class Solution:
  def countWays(self, ranges: List[List[int]]) -> int:
    ranges.sort()
    mx = -1
    mod = 10**9 + 7
    ans = 1
    for start, end in ranges:
      if start > mx:
        ans = ans * 2 % mod
      mx = max(mx, end)
    return ans
class Solution {
  public int countWays(int[][] ranges) {
    Arrays.sort(ranges, (a, b) -> a[0] - b[0]);
    int mx = -1;
    int ans = 1;
    final int mod = (int) 1e9 + 7;
    for (int[] e : ranges) {
      if (e[0] > mx) {
        ans = ans * 2 % mod;
      }
      mx = Math.max(mx, e[1]);
    }
    return ans;
  }
}
class Solution {
public:
  int countWays(vector<vector<int>>& ranges) {
    sort(ranges.begin(), ranges.end());
    int ans = 1, mx = -1;
    const int mod = 1e9 + 7;
    for (auto& e : ranges) {
      if (e[0] > mx) {
        ans = ans * 2 % mod;
      }
      mx = max(mx, e[1]);
    }
    return ans;
  }
};
func countWays(ranges [][]int) int {
  sort.Slice(ranges, func(i, j int) bool { return ranges[i][0] < ranges[j][0] })
  ans, mx := 1, -1
  const mod = 1e9 + 7
  for _, e := range ranges {
    if e[0] > mx {
      ans = ans * 2 % mod
    }
    if mx < e[1] {
      mx = e[1]
    }
  }
  return ans
}

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

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

发布评论

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