AlgoFrames

Count valid subsets

Standard · Write code · about 15 min

There are n items numbered 0 to n - 1. Each pair in conflicts names two items that cannot be chosen together.

Count the subsets of items that contain no conflicting pair. The empty subset counts.

Example 1

Input:

[3,[[0,1]]]

Output:

6

Only {0,1} and {0,1,2} are invalid, so 6 of the 8 subsets count.

Example 2

Input:

[4,[[0,1],[2,3]]]

Output:

9

3 choices for items {0,1} times 3 choices for items {2,3}.

Constraints

  • 1 ≤ n ≤ 16
  • 0 ≤ conflicts.length ≤ n · (n - 1) / 2
  • Each pair holds two different items in the range 0 to n - 1

Learn the idea first: Bitmask DP: TSP and assignments, a visual lesson.

More Bitmask DP: TSP and assignments problems