1807. Fewest Exam Slots
A school must put n exams, numbered 0 to n-1, into time slots. Each entry [u, v] of conflicts says that exams u and v share a student, so they cannot sit in the same slot. Return the smallest number of slots that lets every exam receive a slot with no two conflicting exams sharing one. A pair may be listed more than once or in both orders, which changes nothing, and an exam without conflicts still needs a slot.
First mark which subsets of exams are conflict-free. Then let dp[mask] be one plus the best value of dp for the remaining exams, taken over conflict-free subsets of mask that contain its lowest exam. That enumerates submasks, so the time is O(3^n) with O(2^n) space.
Example 1
- Input:
- n = 3, conflicts = [[0,1],[1,2],[0,2]]
- Output:
- 3
- Explanation:
The three exams conflict pairwise, so each needs its own slot.
Example 2
- Input:
- n = 4, conflicts = [[0,1],[2,3]]
- Output:
- 2
- Explanation:
Exams 0 and 2 can share one slot and exams 1 and 3 another, so 2 slots suffice.
Example 3
- Input:
- n = 1, conflicts = []
- Output:
- 1
- Explanation:
A single exam with no conflicts needs one slot.
Constraints
1 ≤ n ≤ 12
0 ≤ conflicts.length ≤ 100; conflicts[i] = [u, v] with 0 ≤ u, v < n and u ≠ v
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(3^n)
- Space
- O(2^n)