Maximum Number of Achievable Transfer Requests Problem
Description
LeetCode Problem 1601.
We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.
You are given an array requests where requests[i] = [from_i, to_i] represents an employee's request to transfer from building from_i to building to_i.
All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2, there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2.
Return the maximum number of achievable requests.
Example 1:

Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
Output: 5
Explantion: Let's see the requests:
From building 0 we have employees x and y and both want to move to building 1.
From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.
From building 2 we have employee z and they want to move to building 0.
From building 3 we have employee c and they want to move to building 4.
From building 4 we don't have any requests.
We can achieve the requests of users x and b by swapping their places.
We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
Example 2:

Input: n = 3, requests = [[0,0],[1,2],[2,1]]
Output: 3
Explantion: Let's see the requests:
From building 0 we have employee x and they want to stay in the same building 0.
From building 1 we have employee y and they want to move to building 2.
From building 2 we have employee z and they want to move to building 1.
We can achieve all the requests.
Example 3:
Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
Output: 4
Constraints:
1 <= n <= 201 <= requests.length <= 16requests[i].length == 20 <= from_i, to_i < n
Solution
Approach: Try Every Subset of Requests
There are at most 16 requests, so there are at most 216 = 65,536 subsets: few enough to try them all.
Represent a subset as a bitmask mask, where bit i means request i is accepted. A subset is achievable when every building's net change is zero: for each accepted request [from, to], subtract 1 at from and add 1 at to, then check that all counters are 0. We don't need to think about which employee moves where; balanced counts are enough, since the accepted requests then split into cycles.
The answer is the largest number of set bits among achievable masks. A cheap pruning: if a mask has no more bits than the best answer so far, skip the check.
A request with from == to changes nothing, so it is always accepted, as example 2 shows.
Complexity: O(2m · (m + n)) time and O(n) extra space, where m is the number of requests.
class Solution:
def maximumRequests(self, n: int, requests: List[List[int]]) -> int:
m = len(requests)
ans = 0
for mask in range(1, 1 << m):
count = bin(mask).count("1")
if count <= ans:
continue # cannot beat the best subset found so far
net = [0] * n # net change of employees per building
for i, (src, dst) in enumerate(requests):
if mask >> i & 1:
net[src] -= 1
net[dst] += 1
if all(x == 0 for x in net):
ans = count
return ans