Maximum Number of Achievable Transfer Requests - Problem
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] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.
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.
Return the maximum number of achievable transfer requests.
Input & Output
Example 1 — Balanced Transfers
$
Input:
n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
›
Output:
5
💡 Note:
We can accept requests [0,1],[1,0],[0,1],[1,2],[2,0]. Net changes: building 0: -1+1-1+1=1-1=0, building 1: +1-1+1-1=0, building 2: +1-1=0, buildings 3,4 unchanged=0. All buildings balanced.
Example 2 — Simple Case
$
Input:
n = 3, requests = [[0,0],[1,2],[2,1]]
›
Output:
3
💡 Note:
All requests can be accepted: [0,0] is self-transfer (no net change), [1,2] and [2,1] cancel each other out. Net changes all zero.
Example 3 — No Valid Subset
$
Input:
n = 4, requests = [[0,1],[2,3]]
›
Output:
2
💡 Note:
Both requests can be accepted since they involve different pairs of buildings. Building 0: -1, building 1: +1, building 2: -1, building 3: +1. All balanced.
Constraints
- 1 ≤ n ≤ 20
- 1 ≤ requests.length ≤ 16
- requests[i].length == 2
- 0 ≤ fromi, toi < n
Visualization
Tap to expand
Understanding the Visualization
1
Input
Buildings and transfer requests between them
2
Process
Find subset where each building has zero net change
3
Output
Maximum number of achievable requests
Key Takeaway
🎯 Key Insight: Valid transfers require each building to have equal employees leaving and entering
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code