You are given n buildings labeled from 0 to n - 1. Each building initially has a fixed number of employees. During transfer season, employees may request to move from one building to another.
You are given an array requests, where each entry requests[i] = [from, to] indicates a single employee wants to move from building from to building to.
A group of transfer requests is considered valid only when, for every building, the total number of employees leaving equals the total number of employees arriving. In other words, each building must end with the same employee count it started with.
Your task is to return the maximum number of requests that can be fulfilled while still maintaining this balance.
Input: n = 4, requests = [[0,1],[1,2],[2,3],[3,0],[1,0]]
Output: 4
Explanation:
A valid achievable set is: 0→1, 1→2, 2→3, 3→0. These form a full cycle. The remaining request (1→0) breaks balance. So the maximum achievable is 4.
Input: n = 3, requests = [[0,1],[1,2],[2,1],[1,0]]
Output: 3
Explanation:
Possible valid set includes: [0→1], [1→0], [2→1] or [1→2]. Choose any 3 forming a balanced flow.
Input: n = 2, requests = [[0,1],[1,0],[1,0],[0,1]]
Output: 4
Explanation:
Two employees swap buildings twice. All four requests can be satisfied.
Accepted:
Submission: