Skip to content

Practice Maximum Number of Achievable Transfer Requests

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1601
LeetCode Problem

Maximum Number of Achievable Transfer Requests

Problem Statement

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. 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.

Source: Maximum Number of Achievable Transfer Requests on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
Output
5
Explanation

The first five requests form balanced cycles among buildings 0, 1, and 2. The request from 3 to 4 cannot be included because no request restores those buildings’ balances.

# Example 2

Input
n = 3, requests = [[0,0],[1,2],[2,1]]
Output
3
Explanation

The request from 0 to itself changes no balance, and the two requests between 1 and 2 cancel each other. All three can be accepted.

# Example 3

Input
n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
Output
4

Constraints

1 <= n <= 20
1 <= requests.length <= 16
requests[i].length == 2
0 <= fromi, toi < n

Related LeetCode Problems

Practice more problems similar to Maximum Number of Achievable Transfer Requests.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.