Skip to the code
cf.
+25 XP

#453 Fewest People on the Night Shift

medium

A support team splits n people, numbered 0 to n - 1, into a day shift and a night shift. Each pair in conflicts must be on different shifts. Nobody likes night shifts.

Write fewestNightShift(n, conflicts) that returns the smallest possible number of people on the night shift in a valid split, or -1 if no valid split exists. A shift may be empty.

Example 1

Input: n = 5, conflicts = [[0,1],[0,2],[3,4]]
Output: 2

Put 0 on nights and 1 and 2 on days: 1 person. For the pair 3 and 4, one of them must work nights: 1 more. The total is 2.

Example 2

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

A triangle of conflicts can't be split into two shifts.

Constraints

  • 1 <= n <= 10000
  • 0 <= conflicts.length <= 50000
  • 0 <= a, b < n, a !== b
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).