Skip to the code
cf.
+50 XP

#676 Conflict-Free Feature Sets

hard

A build configurator has n optional features, numbered 0 to n - 1. Some pairs can't be enabled together; they are listed in conflicts as [a, b].

Write countValidSets(n, conflicts) that returns how many sets of features (including the empty set) contain no conflicting pair.

n can be 20, so there are over a million sets: check each one in O(1) on average.

Example 1

Input: n = 3, conflicts = [[0,1]]
Output: 6

Of the 8 sets, only {0, 1} and {0, 1, 2} contain the conflict.

Example 2

Input: n = 2, conflicts = []
Output: 4

No conflicts, so all 2^2 = 4 sets are valid.

Constraints

  • 1 <= n <= 20
  • 0 <= conflicts.length <= 190
  • a !== b, no pair listed twice
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).