LeetCode 2580: Count Ways to Group Overlapping Ranges Solution

Master LeetCode problem 2580 (Count Ways to Group Overlapping Ranges), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2580. Count Ways to Group Overlapping Ranges

Problem Explanation

Explanation

To solve this problem, we can use a graph-based approach. We can create a graph where each range is represented by a node, and two nodes are connected by an edge if their corresponding ranges overlap. Once we have this graph, we need to find the number of ways to split the graph into two groups such that any two overlapping ranges belong to the same group.

We can use depth-first search (DFS) to explore the graph and assign each node to one of the two groups. At each node, we have two options: assign it to group 1 or group 2. We continue this process recursively for all connected nodes, making sure that no overlapping ranges end up in different groups. We count the number of valid assignments and return it modulo 10^9 + 7.

Time Complexity: O(N), where N is the number of ranges. Space Complexity: O(N) for storing the graph and group assignments.

Java

Solution Code

class Solution {
    public int countGroups(int[][] ranges) {
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for (int i = 0; i < ranges.length; i++) {
            graph.put(i, new ArrayList<>());
        }
        
        for (int i = 0; i < ranges.length; i++) {
            for (int j = i + 1; j < ranges.length; j++) {
                if (rangesOverlap(ranges[i], ranges[j])) {
                    graph.get(i).add(j);
                    graph.get(j).add(i);
                }
            }
        }
        
        return dfs(0, graph, new int[ranges.length], 1) + dfs(0, graph, new int[ranges.length], 2);
    }
    
    private int dfs(int node, Map<Integer, List<Integer>> graph, int[] groups, int group) {
        groups[node] = group;
        int count = 1;
        for (int neighbor : graph.get(node)) {
            if (groups[neighbor] == 0) {
                if (!rangesOverlap(ranges[node], ranges[neighbor])) {
                    count *= dfs(neighbor, graph, groups, 3 - group);
                } else if (groups[neighbor] == group) {
                    return 0;
                }
            }
        }
        return count;
    }
    
    private boolean rangesOverlap(int[] range1, int[] range2) {
        return range1[0] <= range2[1] && range1[1] >= range2[0];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2580 (Count Ways to Group Overlapping Ranges)?

This page provides optimized solutions for LeetCode problem 2580 (Count Ways to Group Overlapping Ranges) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2580 (Count Ways to Group Overlapping Ranges)?

The time complexity for LeetCode 2580 (Count Ways to Group Overlapping Ranges) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2580 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2580 in Java, C++, or Python.

Back to LeetCode Solutions