2580. Count Ways to Group Overlapping Ranges LeetCode Solution

In this guide, you will get 2580. Count Ways to Group Overlapping Ranges LeetCode Solution with the best time and space complexity. The solution to Count Ways to Group Overlapping Ranges problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Ways to Group Overlapping Ranges solution in C++
  4. Count Ways to Group Overlapping Ranges solution in Java
  5. Count Ways to Group Overlapping Ranges solution in Python
  6. Additional Resources
2580. Count Ways to Group Overlapping Ranges LeetCode Solution image

Problem Statement of Count Ways to Group Overlapping Ranges

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.

See also  879. Profitable Schemes LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

2580. Count Ways to Group Overlapping Ranges LeetCode Solution in C++

class Solution {
 public:
  int countWays(vector<vector<int>>& ranges) {
    constexpr int kMod = 1'000'000'007;
    int ans = 1;
    int prevEnd = -1;

    ranges::sort(ranges);

    for (const vector<int>& range : ranges) {
      const int start = range[0];
      const int end = range[1];
      if (start > prevEnd)
        ans = ans * 2 % kMod;
      prevEnd = max(prevEnd, end);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2580. Count Ways to Group Overlapping Ranges LeetCode Solution in Java

class Solution {
  public int countWays(int[][] ranges) {
    final int kMod = 1_000_000_007;
    int ans = 1;
    int prevEnd = -1;

    Arrays.sort(ranges, (a, b) -> Integer.compare(a[0], b[0]));

    for (int[] range : ranges) {
      final int start = range[0];
      final int end = range[1];
      if (start > prevEnd)
        ans = ans * 2 % kMod;
      prevEnd = Math.max(prevEnd, end);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2580. Count Ways to Group Overlapping Ranges LeetCode Solution in Python

class Solution:
  def countWays(self, ranges: list[list[int]]) -> int:
    kMod = 1_000_000_007
    ans = 1
    prevEnd = -1

    for start, end in sorted(ranges):
      if start > prevEnd:
        ans = ans * 2 % kMod
      prevEnd = max(prevEnd, end)

    return ans
# code by PROGIEZ

Additional Resources

See also  3033. Modify the Matrix LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.