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
Example 2:
Input: n = 3, requests = [[0,0],[1,2],[2,1]]
Output: 3
Example 3:
Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
Output: 4
What is the most efficient algorithm to solve this problem?
This problem asks us to find the maximum number of achievable employee transfer requests, given that the net change in employees for each building must be zero. We can solve this using a brute-force approach by trying all possible combinations of requests and checking if they satisfy the given condition. Since the number of requests is limited to 16, we can use bit manipulation to represent each combination.
Naive Approach:
def maximum_achievable_requests_naive(n, requests):
max_requests = 0
for i in range(1 << len(requests)):
subset = []
for j in range(len(requests)):
if (i >> j) & 1:
subset.append(requests[j])
balance = [0] * n
achievable = True
for request in subset:
balance[request[0]] -= 1
balance[request[1]] += 1
for b in balance:
if b != 0:
achievable = False
break
if achievable:
max_requests = max(max_requests, len(subset))
return max_requests
Optimal Approach (Bit Manipulation with Backtracking):
We can use a more efficient approach by iterating through all possible combinations of requests using bit manipulation. For each combination, we check if it's a valid arrangement by verifying if the net change in employees is zero for each building. This involves creating a balance array to track the change in employee count for each building.
def maximum_achievable_requests(n, requests):
max_achievable = 0
def check_achievability(mask):
balance = [0] * n
count = 0
for i in range(len(requests)):
if (mask >> i) & 1:
balance[requests[i][0]] -= 1
balance[requests[i][1]] += 1
count += 1
for b in balance:
if b != 0:
return 0
return count
for mask in range(1 << len(requests)):
max_achievable = max(max_achievable, check_achievability(mask))
return max_achievable
Big O Analysis:
Edge Cases:
requests
array is empty, the function should return 0, as no requests can be achieved.from_i == to_i
are valid and should be included if they don't violate the net change constraint.n <= 20
and requests.length <= 16
. The exponential time complexity means this solution is only suitable for relatively small inputs. For larger inputs, a different approach might be necessary (possibly involving more complex graph algorithms or optimization techniques).Example Usage:
n = 5
requests = [[0, 1], [1, 0], [0, 1], [1, 2], [2, 0], [3, 4]]
result = maximum_achievable_requests(n, requests)
print(f"Maximum achievable requests: {result}") # Output: 5
n = 3
requests = [[0, 0], [1, 2], [2, 1]]
result = maximum_achievable_requests(n, requests)
print(f"Maximum achievable requests: {result}") # Output: 3
n = 4
requests = [[0, 3], [3, 1], [1, 2], [2, 0]]
result = maximum_achievable_requests(n, requests)
print(f"Maximum achievable requests: {result}") # Output: 4