Ctrl K

Moves Between Offices

Hard
A company has offices offices numbered 0 to offices - 1, and every one of them is packed. Employees have filed move requests: moves[i] = [from, to] means someone wants to move from office from to office to. A set of approved requests works only if every office gets exactly as many people moving in as moving out. A request with from = to changes nothing. Return the largest number of requests that can be approved together.
Example 1
offices=3, moves=[[0, 1], [1, 2], [2, 0], [1, 0]] → 3

The moves 0 → 1, 1 → 2 and 2 → 0 form a loop, so every office stays full. Adding 1 → 0 as well breaks it: two people would enter office 0 and only one would leave.

Example 2
offices=2, moves=[[0, 0], [0, 1]] → 1

A move within the same office changes nothing, so it can be approved. The move 0 → 1 cannot, since nobody moves back.

Example 3
offices=4, moves=[[0, 1], [2, 3]] → 0
Constraints
  • 1 ≤ offices ≤ 20
  • 1 ≤ len(moves) ≤ 16
  • len(moves[i]) = 2
  • 0 ≤ from, to < offices
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Many Witnesses Can Be Honest HardXOR of Every Subset EasyPicks With the Full OR MediumOutshoot the Rival MediumColumns That Cover Rows Medium
loading Python…
Input
offices=3, moves=[[0, 1], [1, 2], [2, 0], [1, 0]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 56 tests on the server, hidden ones included.