LeetCode 2959: Number of Possible Sets of Closing Branches Solution

Master LeetCode problem 2959 (Number of Possible Sets of Closing Branches), a hard 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.

2959. Number of Possible Sets of Closing Branches

Problem Explanation

Explanation

To solve this problem, we can use a bitmask dynamic programming approach. We will iterate through all possible subsets of branches and for each subset, we will check if all branches in that subset are reachable within the given maxDistance. We will use a bitmask to represent the subset of branches.

  1. Initialize a 2D array dp where dp[mask] represents the number of possible sets of closing branches for the subset of branches represented by the bitmask mask.
  2. For each subset represented by a bitmask mask, iterate through all branches i that are not closed. Check if branch i can be added to the subset represented by mask without violating the maxDistance constraint.
  3. Update the dp array accordingly.
  4. Finally, return the result stored in dp[(1 << n) - 1], which represents all branches being active.

Time complexity: O(2^n * n^2), where n is the number of branches
Space complexity: O(2^n)

Solution Code

class Solution {
    public int numberOfSets(int n, int maxDistance, int[][] roads) {
        int MOD = 1000000007;
        int[][] dp = new int[1 << n][n];
        dp[1][0] = 1;
        
        for (int mask = 1; mask < (1 << n); mask++) {
            for (int i = 0; i < n; i++) {
                if ((mask & (1 << i)) == 0) continue;
                
                for (int j = i + 1; j < n; j++) {
                    if ((mask & (1 << j)) == 0) continue;
                    
                    if (roads[i][j] <= maxDistance) {
                        dp[mask | (1 << j)][j] = (dp[mask | (1 << j)][j] + dp[mask][i]) % MOD;
                    }
                }
            }
        }
        
        int result = 0;
        for (int i = 0; i < n; i++) {
            result = (result + dp[(1 << n) - 1][i]) % MOD;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2959 (Number of Possible Sets of Closing Branches)?

This page provides optimized solutions for LeetCode problem 2959 (Number of Possible Sets of Closing Branches) 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 2959 (Number of Possible Sets of Closing Branches)?

The time complexity for LeetCode 2959 (Number of Possible Sets of Closing Branches) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2959 on DevExCode?

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

Back to LeetCode Solutions