Maximum Number of Achievable Transfer Requests
10 Prerequisite LeetCode Problems
“1601. Maximum Number of Achievable Transfer Requests” is a combination of a bitmask and graph theory problem. Here are 10 problems to prepare:
78. Subsets: This problem introduces you to the concept of bit manipulation to generate all possible subsets.
200. Number of Islands: This problem is a fundamental graph problem that will help you understand basic graph traversal techniques.
207. Course Schedule: This problem introduces the concept of directed graphs, which is essential to understand how to tackle problem 1601.
210. Course Schedule II: This is a continuation of problem 207, it also introduces a new concept of generating a topological sort.
133. Clone Graph: This problem will give you a better understanding of how to handle and manipulate graphs.
279. Perfect Squares: This problem introduces the concept of BreadthFirst Search in graphs.
994. Rotting Oranges: This problem is an example of a multisource BreadthFirst Search and can help you understand how to handle multiple nodes simultaneously.
841. Keys and Rooms: This problem involves graph traversal, similar to problem 1601, but with a simpler setup.
1345. Jump Game IV: This is a graph problem which is solved using BreadthFirst Search, similar to problem 1601.
79. Word Search: This problem introduces DepthFirst Search on a 2D grid, which is an important technique to handle graph problems.
The combination of graph traversal techniques, the concept of directed graphs, bit manipulation, and understanding of BreadthFirst Search and DepthFirst Search will prepare you to tackle “1601. Maximum Number of Achievable Transfer Requests”.


Problem Classification
This problem can be classified under the domain of “Graph Theory” and “Combinatorial Optimization”. The problem of transfer season in the buildings, and the nature of requests where an employee from one building requests to move to another building can be viewed as a directed graph where nodes represent buildings and directed edges represent requests for transfer from one building to another.
‘What’ components of the problem:
Number of Buildings (n): The problem provides the total number of buildings which is a fixed number from 0 to n1.
Employee Transfer Requests: The problem provides a list of requests. Each request is represented as a pair of [fromi, toi], where ‘fromi’ and ’toi’ are the building numbers from which and to which the employee wants to move respectively.
Feasibility of requests: The requests are feasible only if the number of employees leaving a building is equal to the number of employees moving in, for each building.
Maximum Achievable Requests: The problem asks for the maximum number of achievable requests given the constraints.
We can further classify this problem as an “Optimization Problem” in the field of “Operations Research”. Specifically, it is a “Maximum Matching Problem” in “Bipartite Graphs”, where each edge of the graph represents a potential employee transfer request, and our goal is to maximize the number of edges (i.e., requests) that can be satisfied while maintaining the balance of incoming and outgoing transfers at each building (i.e., node). However, this is a special case where each node can be connected to itself, and the graph is directed, so it’s not strictly a bipartite graph problem.
Therefore, it is an optimization problem where we want to maximize the number of feasible transfer requests. We may have to employ techniques related to combinatorial optimization, graph theory, or network flows to come up with an efficient solution.
Visual Model of the Problem
To visualize this problem, one effective way is to use a directed graph, where nodes represent buildings, and directed edges represent requests for transfer from one building to another.
For example, if we take a problem instance where there are 5 buildings numbered from 0 to 4 and we have 6 requests represented as [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]], we can represent it as the following directed graph:
0 > 1 < 1
^  ^
 v 
> 2 > 0


v
1 > 2
^


3 > 4
Each arrow denotes a request of an employee wanting to move from one building to another. For example, the arrow 0 > 1 means there is an employee in building 0 who wants to move to building 1.
To satisfy the condition of net change in employee transfers being zero, it means for every building, the number of outgoing arrows should be equal to the number of incoming arrows.
Our goal is to make as many of these requests possible while ensuring the net change in transfers is zero for each building, which can be visualized as trying to form cycles in the graph, as every cycle represents a way to fulfill a set of requests without breaking the zero net transfer condition. For example, in the above graph, there’s a cycle among nodes 0, 1, and 2, which can be used to satisfy three requests.
Visualizing this problem can be done best using a directed graph. Here’s how you can do it:
 Treat each of the n buildings as a node in the graph.
 For each request from building “fromi” to building “toi”, draw a directed edge from node “fromi” to node “toi”.
Let’s take an example with n=3, and requests = [[0,1],[1,2],[2,0],[1,2],[1,2]]. This is how we can visualize it:
0 > 1 > 2 > 0

+> 2
+> 2
In this graph, there is one cycle: 0 > 1 > 2 > 0. Also, we have two additional requests from 1 to 2. Since we can satisfy a request multiple times between two buildings, we can satisfy four requests here: one for each edge in the cycle, and one of the two additional requests from 1 to 2.
When we add more buildings and requests, the graph will become more complex with multiple separate cycles and additional edges. But the principle remains the same: we’re looking for the maximum number of requests that can be fulfilled, which will correspond to the maximum number of edges we can traverse in this graph, visiting each node (building) the same number of times we leave it.
Remember, the visualization is merely a tool to help us better understand the problem. The actual solution doesn’t involve drawing or manipulating a graph but rather using bit manipulation and depthfirst search to simulate the process of fulfilling requests in an efficient manner.
Visualizing each example given in the problem using a directed graph can be a useful way to understand the problem better. Let’s take a look at each of them:
Example 1: Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]] Output: 5
We can visualize this as:
0 > 1 > 2 > 0 (cycle)
^  V
 +> 0
 ^
 
++
3 > 4
In this graph, the cycle 0 > 1 > 2 > 0 has 3 requests. Additional requests 0 > 1 and 1 > 0 can also be satisfied. One request from building 3 to 4 can also be fulfilled. This makes total of 5 requests, which is our answer.
Example 2: Input: n = 3, requests = [[0,0],[1,2],[2,1]] Output: 3
We can visualize this as:
0 > 0
1 > 2 > 1 (cycle)
Here, the cycle 1 > 2 > 1 has 2 requests and the additional request 0 > 0 is also fulfilled. So the maximum number of requests fulfilled is 3.
Example 3: Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]] Output: 4
We can visualize this as:
0 > 3 > 1 > 2 > 0 (cycle)
Here, we have a cycle that goes through all buildings, and each of the edges in the cycle corresponds to a request. Therefore, we can satisfy all 4 requests.
Remember, these visualizations are tools to understand the problem and its examples better. The actual solution involves using bit manipulation and depthfirst search to compute the maximum number of achievable requests.
Problem Restatement
We have a number of buildings, each identified by a unique number and populated with a certain number of employees. Now, some of these employees wish to relocate from their current building to a different one. The specifics of these requests are given to us as an array, where each element of the array signifies a transfer request with two numbers  the first number represents the current building of the employee, and the second number is the building they wish to move to.
However, there’s a catch. Each building needs to maintain the same number of employees, meaning any employee who leaves a building needs to be replaced by another employee coming from a different building. Essentially, for every building, the total number of employees coming in must be equal to the total number leaving.
Our task is to figure out the maximum number of these transfer requests that can be fulfilled while ensuring the conditions are met. In other words, we need to find a way to move the employees around so that the most number of them get their preferred building, while still maintaining the same number of employees in each building.
We are provided with constraints that the number of buildings (n) is between 1 and 20, and the number of requests can be up to 16. Furthermore, all buildings and requests are valid; meaning building numbers are within the valid range and each request has a valid ‘from’ and ’to’ building.
Abstract Representation of the Problem
Let’s abstract away the realworld details and focus on the essential problem structure.
We have a set of elements (employees), each belonging to a distinct category (buildings). The elements have preferences to change their category, defined by pairs of source and target categories (transfer requests). However, any movement of elements must satisfy the constraint that the total count of elements in each category must remain constant, i.e., for every element leaving a category, there must be another one entering it.
Our goal is to optimize the number of elements that can change their category according to their preference, while respecting the stated constraint.
In a more mathematical context, this problem could be seen as finding an optimal matching in a directed graph where vertices are the categories, edges represent the preferences, and the constraint is that the indegree and outdegree of each vertex must remain the same.
Can we replace elements by objects and category with groups?
Yes, definitely. The abstract representation can be further simplified as follows:
We have a set of objects, each belonging to a distinct group. The objects have preferences to change their group, defined by pairs of source and target groups. However, any movement of objects must satisfy the constraint that the total count of objects in each group must remain constant, i.e., for every object leaving a group, there must be another one entering it.
Our goal is to optimize the number of objects that can change their group according to their preference, while respecting the stated constraint.
This might make it more clear and is especially useful if “elements” and “categories” seem too specific or linked to a particular domain. The terms “objects” and “groups” are more general.
Terminology
The main specialized terms and concepts in this problem relate to graph theory and combinatorial optimization:
Requests: In the problem statement, requests represent directed edges in a graph. Each request is a pair [fromi, toi] where an employee wants to move from building ‘fromi’ to building ’toi’.
Achievable Requests: Achievable requests refer to the scenario where the total number of employees in each building remains the same after some requests are fulfilled. In graph theory, this can be seen as finding a subset of edges (requests) that maintain the degree balance in each node (building).
Maximum Achievable Requests: This is the optimization goal. Out of all possible combinations of requests that keep the building populations stable, we want to find the combination that includes the maximum number of requests.
Combinatorial Optimization: This problem can be seen as an instance of combinatorial optimization, which involves finding the best solution from a finite set of possible solutions. Here, we want to find the maximum number of achievable requests.
Bitmasking: To solve this problem efficiently, a technique called bitmasking can be used. This involves using binary digits to represent subsets of requests. It is a standard technique for solving problems where we need to check all combinations or subsets of a certain size.
Problem Simplification and Explanation
Let’s break this problem down and use a metaphor to understand it better:
Imagine you are managing a large system of banks (the “buildings” in the problem). Every bank has a fixed number of employees, and during the transfer season, some of the employees want to transfer from one bank to another (the “requests”).
However, every bank needs to maintain its current number of employees for operations to run smoothly. This means that for every employee that leaves a bank, another employee should enter it, maintaining the balance.
The main task here is to fulfill as many transfer requests as possible while maintaining the balance in each bank. If you consider each bank as a node and each transfer request as a directed edge from one node to another, this problem becomes a graph problem.
To simplify even further, imagine a small system of 3 banks A, B, C. Each bank has 1 employee, and there are transfer requests from A to B, B to C, and C to A. If you fulfill all these requests, each bank will still have 1 employee (though not the same ones as before), and you’ve fulfilled all requests. But if there was another request from A to B, you could not fulfill this one without disturbing the balance, because B would then have 2 employees and A none.
So, the problem becomes finding the most “circuits” or “loops” of transfers you can arrange, where every bank that “loses” an employee also gains one, keeping the total number constant.
This problem is mainly about balancing needs and resources, a common theme in optimization and graph theory problems. And you can use several strategies like combinatorics, depthfirst search, or dynamic programming to tackle it.
Constraints
Let’s consider these constraints:
1 <= n <= 20 1 <= requests.length <= 16 requests[i].length == 2 0 <= fromi, toi < n
Number of Buildings (n): The number of buildings is between 1 and 20. The relatively small range means we may be able to use a solution with higher time complexity, such as those involving permutations or subsets, since they won’t exceed computational limits.
Number of Requests: There are between 1 and 16 requests. Again, this relatively small range allows for solutions with higher time complexity. For instance, checking all possible combinations of requests.
Format of Requests: Each request is a pair of integers
[fromi, toi]
, withfromi
andtoi
representing the buildings where the employee is currently located and where they wish to move, respectively. It’s worth noting that employees can request to move from a building to the same building, i.e.,fromi
can be equal totoi
. This condition can be exploited by fulfilling these requests directly since they don’t require any matching requests.Building Indices: Both
fromi
andtoi
are between 0 and n  1. The numbering of buildings from 0 to n  1 can be used to our advantage for quick lookups or maintaining counts in an array or list indexed by building number.
To summarize, the constraints suggest that an exhaustive search might be a suitable approach given the small values of n and the number of requests. Furthermore, the format of requests and building indices can be used for easy management of counts and fulfillment of requests.
In the problem statement, there’s no explicit mention of whether fromi
can be equal to toi
. However, the problem does not specifically prohibit such cases either. In problem Example 2:
Input: n = 3, requests = [[0,0],[1,2],[2,1]] Output: 3
We see a request where fromi
and toi
are the same ([0,0]). Therefore, it appears that requests where an employee wants to stay in the same building are allowed based on this example.
Identification of Applicable Theoretical Concepts
This problem can be solved using the concept of “Depth First Search” (DFS) from graph theory, a method for traversing or searching tree or graph data structures. Here, DFS can be used to explore all permutations of request selections to find the one that satisfies the problem’s conditions.
In addition, this problem also involves the use of “bit masking”, a technique that can help to manage and manipulate sets of data efficiently. Bit masking can be used to represent all possible combinations of requests and help to identify the maximum number of achievable requests.
Here are the steps using DFS and bit masking:
Create a bit mask for all requests, where each bit of the mask represents whether a request is selected (1) or not (0).
Use DFS to traverse all possible combinations of requests represented by the bit mask.
For each combination of requests, calculate the number of incoming and outgoing transfers for each building.
Check if the net change in transfers for each building is zero. If it is, the current combination of requests is achievable, and you update the maximum number of achievable requests accordingly.
Continue the DFS until all combinations of requests have been checked, and return the maximum number of achievable requests found.
This algorithm exploits the fact that the number of buildings (n) and the length of requests are relatively small (n <= 20, requests.length <= 16), which makes it feasible to use DFS and bit masking to check all possible combinations of requests.
Inference of ProblemSolving Approach from the Problem Statement
The use of Depth First Search (DFS) isn’t directly indicated in the problem statement, but it’s an inference based on the problem’s nature and requirements. Here are the reasons why DFS might be a good fit:
Exploration of all possibilities: The problem asks for the “maximum number of achievable requests”. To find this maximum, we need to consider all possible combinations of requests, something DFS excels at.
Combination Problem: The problem is a combination problem where we need to select a group of requests from a larger set. DFS is a standard approach to solve combination problems because it can exhaustively search all possible combinations.
Feasibility: Given the problem constraints (n <= 20, requests.length <= 16), it is feasible to perform DFS because the search space is relatively small. DFS can be computationally expensive for larger inputs, but for this problem, it is manageable.
Remember, selecting the right algorithm often involves understanding the problem’s requirements and constraints and drawing upon knowledge of various problemsolving strategies and how well they might apply. The problem statement doesn’t tell us what algorithm to use, but by understanding the problem, we can make an informed decision.
The inference about using a bit mask for this problem comes from a few observations in the problem statement and constraints:
Limited Number of Requests: The problem states that the length of requests is at most 16. Since there are a limited and relatively small number of requests, we can represent each possible subset of requests using a bit mask.
Representing Subsets: In a bit mask, each bit can represent the presence (1) or absence (0) of a request in a subset. For example, if we have 4 requests and we represent a subset using a bit mask ‘1010’, it means the first and third requests are included in the subset, while the second and fourth are not.
Efficient Computation: The problem requires us to find the maximum number of achievable requests. Using a bit mask allows us to efficiently compute all possible subsets of requests and track the maximum.
Use in DepthFirst Search (DFS): Bit masks are often used in DFS, especially when dealing with problems requiring us to explore all possible combinations or permutations. Since we inferred DFS could be a suitable approach for this problem, the use of bit masks fits naturally into this strategy.
It’s important to note that the usage of bit masks or any other specific data structures or algorithms is not explicitly mentioned in the problem statement. The decision to use them comes from experience with similar problems, understanding of their benefits and suitability to the problem at hand.
Stepwise Refinement
Let’s apply the concept of stepwise refinement to our current problem.
Problem Statement
Determine the maximum number of achievable requests for employees wanting to transfer between buildings. There are ’n’ buildings and ‘requests’ array where requests[i] = [fromi, toi] represents an employee’s request to transfer from building fromi to building toi.
Initial Breakdown into Steps
 Declare and initialize variables.
 Generate all subsets of requests.
 Check each subset if it results in a balance of transfers for every building.
 Track the maximum number of achievable requests.
Detailed Breakdown
Declare and Initialize Variables
 Declare a variable to track the maximum number of achievable requests, initialized as 0.
 Declare a variable to store the number of requests.
 Declare an array to keep track of the balance of transfers for each building.
Generate all Subsets of Requests
 This can be achieved by a depthfirst search (DFS) approach where each request can be either included or excluded.
 Start DFS from the first request, mark it as either included or excluded, and then move on to the next request.
 Repeat this process until all requests have been processed.
Check Each Subset if it Results in a Balance of Transfers for Every Building
 For each subset of requests generated by DFS, check if it results in a balance of transfers for every building.
 Balance of transfers for a building is achieved when the number of employees leaving is equal to the number of employees moving in.
 Use an array to keep track of the balance for each building.
Track the Maximum Number of Achievable Requests
 After checking the balance for each subset, if the subset is balanced, compare its size (number of requests) with the current maximum.
 If it’s greater, update the maximum.
At this level of detail, the pseudocode can be translated to real code. This provides a clear strategy for the problem. The next step is to refine this further into actual implementation.
 Breaking down the highlevel solution into granular, actionable steps:
Here’s one way to break down the problem:
a. Understand the problem: We are given a list of requests where each request is a transfer request from one building to another. The goal is to find the maximum number of requests that can be satisfied such that the net change in employees for each building is zero.
b. Track the balance of each building: We need to keep track of the number of outgoing and incoming requests for each building.
c. Generate all subsets of requests: We will generate all possible subsets of requests to check which combination gives us the maximum number of achievable requests.
d. Check if a subset of requests is achievable: For each subset, we will calculate the total incoming and outgoing requests for each building. If for all buildings, the total outgoing requests is equal to the total incoming requests, then the subset is achievable.
e. Find the maximum: Among all achievable subsets of requests, we will keep track of the subset with the maximum number of requests.
 Identifying independent parts:
Tracking the balance of each building: This can be done independently as we just need to iterate over all requests and increase the count for outgoing and incoming requests for each building.
Generating all subsets: This operation is independent of the actual requests and only depends on the total number of requests.
Checking if a subset is achievable: This operation is independent for each subset and can be done concurrently.
 Identifying repeatable patterns:
For every subset, we are repeating the same operation of counting total outgoing and incoming requests for each building and comparing them.
The process of checking if a subset is achievable or not is repeated for each possible subset.
These patterns suggest that we can use techniques like bit manipulation for generating subsets and depthfirst search or backtracking to go through all possible subsets.
Solution Approach and Analysis
 Understanding the problem and formulating the approach:
The problem presents us with multiple “requests”, each representing an employee’s desire to transfer from one building to another. The task is to find the maximum number of requests that can be fulfilled while ensuring that each building maintains the same number of employees. This essentially means that for every building, the number of employees leaving must be equal to the number of employees coming in.
An intuitive way to view this problem is like a system of reservoirs and pipes, where each building represents a reservoir and each request represents a pipe transferring water (employees) from one reservoir to another. The problem is then to find the maximum number of pipes that can be turned on without causing an imbalance in the amount of water in each reservoir.
 Breaking down the solution:
a) Representation of requests: We represent each request as a pair (fromi, toi). It’s important to note that multiple requests can have the same ‘fromi’ or ’toi’, and it’s also possible for ‘fromi’ to equal ’toi’, meaning an employee can request to stay in their current building.
b) Generation of all possible combinations of requests: Given that there could be up to 16 requests, it is feasible to generate all possible combinations of requests. This can be done using bit manipulation, where each bit in a 16bit integer represents whether a particular request is included (1) or not (0) in a combination. We iterate from 0 to 2^n (where n is the number of requests), treating each integer in between as a different combination of requests.
c) Checking each combination: For each combination, we track the net flow of employees for each building. This can be done by initializing an array of size ’n’ (number of buildings) with all elements as 0 and then incrementing the element at index ‘fromi’ and decrementing the element at index ’toi’ for each request in the combination. If at the end of this process, all elements in the array are 0, this means the combination is valid, and we update our answer with the number of requests in this combination if it’s greater than the current answer.
 How changes in the problem’s parameters would affect the solution:
The complexity of the solution is heavily dependent on the number of requests. As the number of requests increases, the number of combinations we need to check grows exponentially. So, the approach is more suitable for smaller inputs.
If the number of buildings increases, the time taken to check each combination also increases, but this effect is less pronounced than the effect of the number of requests.
 Example:
Let’s consider the first example from the problem: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]].
Here, if we consider all combinations, one valid combination would be [[0,1],[1,0],[0,1],[1,2],[2,0]], which includes 5 requests. The corresponding change in employee counts for the buildings would be [0, 0, 0, 1, 1], which means that the total incoming and outgoing employees for each building (except 3 and 4) are balanced.
This is the maximum number of requests that can be satisfied, and hence the output for this case would be 5.
Thought Process
Let’s start breaking down the problem.
Problem Understanding and Cues:
The problem tells us we have n
buildings and some transfer requests. Each request is a movement from one building to another. The task is to find the maximum number of requests we can fulfill while keeping the number of employees in each building constant.
Key points to note:
 We are asked for the maximum number of requests we can fulfill, not all.
 Fulfilling a request means transferring one employee from one building to another.
 Each building must have the same number of employees before and after fulfilling the requests.
These cues suggest that we need a way to test different combinations of requests to find the maximum that can be fulfilled while maintaining balance in each building. A bruteforce approach might be to generate all possible subsets of requests and verify each one.
Approach to the Problem: This problem can be approached as a subset generation problem where we generate all possible subsets of requests (using bit manipulation) and check which one satisfies the condition. The condition being the number of employees in each building remains the same.
We can solve this problem using depthfirst search (DFS) and bit manipulation. We will create all possible subsets of requests (represented by bitmasks), and for each subset, we check if it satisfies the building balance condition. If it does, we update our answer with the number of requests in that subset.
Insights:
 We can’t fulfill a request if it makes the number of employees in the source building negative or makes the number of employees in the target building more than it originally had.
 The order in which we fulfill the requests doesn’t matter because we’re asked for the maximum number that can be fulfilled, not the sequence to fulfill them.
Python Code:


In this code, 1 << num_requests
generates a binary number with num_requests
bits all set to 1, which represents all possible subsets when treated as a bitmask. For each subset, we iterate through all requests and use bit manipulation (i >> j) & 1
to check if the jth request is included in the subset. If it is, we update the buildings
array accordingly. After checking all requests, if the buildings
array has all elements as zero, this means the subset satisfies the condition and we update max_requests
with the number of requests in the subset.
From Brute Force to Optimal Solution
Sure, let’s start with a bruteforce solution.
Bruteforce Approach:
A naive approach to this problem would be to generate all possible subsets of the requests and check each subset to see if it fulfills the condition of the problem, i.e., the number of employees in each building remains the same after executing the requests in the subset. If it does, then we calculate the size of this subset. The maximum size among all such valid subsets would be our answer.
This bruteforce approach can be done using recursion or backtracking to generate all subsets and verify each one. However, it’s highly inefficient because it has to generate 2^n subsets, where n is the number of requests, and check each subset, which takes O(n) time. So, the total time complexity is O(n*2^n).
This approach also needs O(n) space to store the temporary subset during recursion or backtracking.
Optimization:
This problem can be optimized using bit manipulation and depthfirst search (DFS).
We realize that each subset of requests can be represented as a bitmask where each bit corresponds to whether a particular request is included in the subset or not. For instance, if we have 3 requests, then a subset including the first and third requests can be represented by the bitmask 101.
With this idea, we can use a loop from 0 to 2^n1 to generate all possible bitmasks, which represent all possible subsets of requests. For each bitmask, we check if it forms a valid subset (i.e., after executing the requests in this subset, the number of employees in each building remains the same), and if it does, we update our answer with the number of requests in this subset (which is the number of set bits in the bitmask).
This approach eliminates the need for recursion or backtracking, which significantly reduces the space complexity to O(1), as we only need space to store the buildings’ states and the answer.
The time complexity is still O(n*2^n), but it runs faster in practice than the bruteforce approach due to lower overhead and better constant factors.
Coding Constructs
Highlevel Problemsolving Strategies or Techniques: The code utilizes a combination of bit manipulation, depthfirst search (DFS), and exhaustive search (i.e., considering all possible combinations of requests). Bit manipulation is used to represent subsets of requests, DFS is used to check if a subset of requests is valid, and exhaustive search is done over all possible subsets to find the maximum number of achievable requests.
Explaining the Code to a Nonprogrammer: Think of this as a task assignment problem, where each task represents a transfer request of moving an employee from one building to another. We are trying to figure out the maximum number of tasks that can be completed while ensuring that, after all tasks are completed, the same number of employees remain in each building as initially. The code basically explores all possible combinations of task assignments (like flipping a coin for each task to decide whether to include it or not) and keeps track of the largest valid group of tasks it has seen.
Logical Elements or Constructs: The logical elements used in this code include control structures such as forloops and ifelse statements, arithmetic operations like addition and subtraction, bitwise operations such as shift and bitwise AND, function calls (specifically recursive function calls), and usage of lists (arrays) to store and manipulate data.
Algorithmic Approach in Plain English: For each possible combination of requests (represented by binary numbers), the code checks if performing these requests would result in the same number of employees in each building. It does this by tracking the number of employees leaving and arriving at each building. If, for a combination, the number of incoming and outgoing employees for each building matches, the code checks how many requests are in this combination (how many ‘1’s are in the binary number). If it’s more than any previously checked valid combination, it updates the maximum number of achievable requests.
Key Steps or Operations on the Input Data:
 Iteration over all possible combinations of requests (binary numbers from 0 to 2^n  1).
 For each combination, using DFS to check if the combination is valid (i.e., the buildings’ states remain balanced after these requests are performed).
 Counting the number of ‘1’s in the binary number (which represents the number of requests in this combination).
 Updating the maximum number of achievable requests if the current combination is valid and has more requests than the previous maximum.
Algorithmic Patterns or Strategies: The code uses a bit manipulation strategy to generate all possible subsets of requests. It uses a recursive depthfirst search pattern to check if a subset of requests is valid. And it uses the exhaustive search strategy to check all possible subsets and find the maximum one.
Language Agnostic Coding Drills
Dissecting the Code into Distinct Concepts:
 Arrays (or Lists): Data structure to store and manipulate collections of items. They are used here to represent the requests and track the states of the buildings.
 Loops: A control structure that allows code to be executed repeatedly. Used for iterating over possible combinations and the requests within each combination.
 IfElse Conditional Statements: Control structure to allow branching logic based on certain conditions. Used to check if a combination is valid and if it contains more requests than the current maximum.
 Bit Manipulation: Techniques for directly manipulating bits. Used here to represent subsets of requests and count the number of requests in a subset.
 Function Calls: Using a function to encapsulate a block of code that can be reused. Here, a recursive function is used to implement depthfirst search.
 Recursion: A method where the solution to a problem depends on solutions to smaller instances of the same problem. It’s used in DFS for exploring different subsets of requests.
 DepthFirst Search (DFS): An algorithm for traversing or searching tree or graph data structures. Used to check if a subset of requests is valid.
Concepts Listed in Order of Increasing Difficulty:
 Arrays (or Lists): Basic data structure, foundational knowledge for programming.
 Loops: Another basic concept, slightly more complex as it involves control flow.
 IfElse Conditional Statements: Also a basic concept, but requires logical reasoning to define the conditions correctly.
 Function Calls: Intermediate concept, requires understanding of code reuse and scope.
 Bit Manipulation: Intermediate concept, involves understanding binary representation and bitwise operations.
 Recursion: Advanced concept, requires good understanding of control flow, stack, and breaking down problems.
 DepthFirst Search (DFS): Advanced concept, requires understanding of recursion, tree/graph data structures, and algorithms.
Problemsolving Approach:
 Understanding the Problem: Understand that we need to find the maximum number of requests that can be fulfilled such that each building ends up with the same number of employees as it started with.
 Array Manipulation: Recognize that the requests can be represented as a list, and the state of the buildings can be tracked using an array.
 Exhaustive Search with Bit Manipulation: Realize that the problem requires checking all possible subsets of requests. Use bit manipulation to represent these subsets and iterate over them.
 DFS with Recursion: To check if a subset of requests is valid, implement a depthfirst search using recursion. The DFS explores different arrangements of requests and validates if they keep the buildings balanced.
 Conditionals and Loops: Use conditional statements to check if a subset of requests is valid and if it contains more requests than the current maximum. Use loops to iterate over all possible combinations and the requests within each combination.
 Piecing It Together: Assemble all these concepts to form the final solution. Use the data structure to hold requests, use bit manipulation to generate subsets, use DFS to validate subsets, and use conditionals and loops to find the maximum valid subset.
Targeted Drills in Python
Individual Python Coding Drills for Each Identified Concept:
Arrays (or Lists):
1 2 3 4 5
# Initialize an array (list) in Python array = [1, 2, 3, 4, 5] # Access elements in the array print(array[2]) # Output: 3
Loops:
1 2 3
# Loop through an array for element in array: print(element)
IfElse Conditional Statements:
1 2 3 4 5 6
# Conditional statement example num = 10 if num > 5: print("Greater than 5") else: print("Not greater than 5")
Function Calls:
1 2 3 4 5 6
# Define a function def say_hello(name): return f"Hello, {name}!" # Call the function print(say_hello("Alice"))
Bit Manipulation:
1 2 3
# Count the number of set bits (1s) in a number num = 10 # binary representation: 1010 print(bin(num).count('1')) # Output: 2
Recursion:
1 2 3 4 5 6 7 8
# Calculate factorial using recursion def factorial(n): if n == 0: return 1 else: return n * factorial(n1) print(factorial(5)) # Output: 120
DepthFirst Search (DFS):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# DFS on a binary tree class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def dfs(node): if node is not None: print(node.val) dfs(node.left) dfs(node.right) root = Node(1) root.left = Node(2) root.right = Node(3) dfs(root) # Output: 1 2 3
ProblemSpecific Concepts and Drills:
State Representation and Validation:
1 2 3 4 5 6 7 8 9 10 11
# Represent and validate building states states = [0, 0, 0] # State for each building requests = [[0, 1], [1, 2], [2, 0]] # Array of requests def is_valid(requests, states): for from_building, to_building in requests: states[from_building] = 1 states[to_building] += 1 return all(state == 0 for state in states) print(is_valid(requests, states)) # Output: True
This drill encapsulates the concept of representing and validating the state of the buildings after a set of requests is fulfilled. It’s essential for our problem as it allows us to check if a certain set of requests can be fulfilled while keeping the number of employees in each building balanced.
Integration of Drills:
 Begin by using the array/list concept to represent the initial state of each building and the list of requests.
 Use bit manipulation and loops to generate all possible subsets of requests.
 For each subset, use the state representation and validation concept to check if it can be fulfilled.
 Use a recursive function (DFS) to explore different arrangements of requests within each subset.
 Use conditional statements inside the DFS function to keep track of the subset with the maximum number of valid requests.
 Piece together all these parts following the approach and solution explained in the previous discussions.
Q&A
Why can’t we do it using minimum of indegree and outdegree of the node?
That’s a good question, and this approach of using the minimum of indegree and outdegree of a node seems to make sense at first glance because intuitively, you could fulfill as many requests as the minimum of indegree and outdegree.
However, this approach won’t work in all cases because the order of the requests matter. Let’s take an example to illustrate this:
Suppose we have 3 buildings (0, 1, 2) and 4 requests:
[0, 1]
[1, 2]
[0, 1]
[2, 0]
If you look at the minimum of indegree and outdegree, all buildings have 2 incoming and 2 outgoing requests. So, you might think we can fulfill all 4 requests. But if you try to fulfill the requests in the order they’re given, after the first 3 requests, building 0 will have no employees left, so the last request can’t be fulfilled.
On the other hand, if you rearrange the requests like this:
[0, 1]
[0, 1]
[1, 2]
[2, 0]
Now, you can fulfill all 4 requests.
So, the problem is more complex than just looking at the minimum of indegree and outdegree for each node, because the order of the requests matter. Therefore, we need to consider all possible arrangements of the requests, which is why we use a DFS with bit manipulation to generate all subsets of requests, and then check which subset can fulfill the maximum number of requests.
can you elaborate this example a bit i am also stuck at this since [1,1],[1,1] means two employes leaves and again they enter that means 2 request fullfilled.
The key point is that a request [a, b]
means an employee moves from building a
to building b
. To fulfill a request, the building a
must have at least one employee.
Consider your example [1,1],[1,1]
, these two requests mean two employees leave building 1
and reenter building 1
. Initially, we must have at least two employees in building 1
to fulfill both requests.
Suppose building 1
starts with only one employee. We can fulfill the first request [1,1]
because there’s an employee in building 1
who can leave and reenter. But after this request, there’s still only one employee in building 1
. Now if we try to fulfill the second request [1,1]
, we can’t, because we don’t have a second employee in building 1
to leave and reenter. Therefore, in this situation, we can only fulfill one request, not two.
constraints clearly tell us that this problem can be solved by brute force
Yes, you’re correct. Given the problem constraints (i.e., 1 <= n <= 20 and 1 <= requests.length <= 16), it’s indeed feasible to apply a brute force solution.
The number of buildings n
is small (up to 20), and the number of requests is also quite small (up to 16). This means the number of possible states to explore isn’t prohibitively large.
A brute force approach would entail generating all possible subsets of requests and checking each one to see if it leaves all buildings balanced. While this would certainly work, it’s worth noting that this approach has a time complexity of O(2^n), which can be acceptable due to the constraints provided.
That being said, it’s still beneficial to find a more optimized solution, such as one using depthfirst search (DFS) or dynamic programming (DP). Such solutions can provide more efficient and scalable algorithms for problems with similar structures but larger inputs.
Remember, while solving problems within given constraints is important, considering scalability and performance is also crucial in realworld applications.
I think this is a Minimum Cost Maximum Flow problem.
Firstly, we should calculate # of redundant requests and # of absent requests.
Then we can construct a graph, G = <V, E>
V = {S, T, Factory_1, Factory_2, …, Factory_n} E = { (S > Factories that have redundant requests), (Facotries that need requests > T, All other edges provided by function } The cost of each unit is 1. Then we can calculate Minimum Cost Maximum Flow of G.
of requests  minimum cost is answer.
I didn’t implement it but think it may be a possible solution.
Is there more efficient solution for this problem?
The approach you’ve described can certainly be used to solve this problem and is a great demonstration of the flexibility of network flow models. In fact, the problem of finding the maximum number of requests that can be fulfilled is indeed a variation of the Maximum Flow Problem.
However, the key challenge here is the construction of the network graph. We need to accurately represent the conditions of the problem within the graph. Here, it’s not as simple as having a surplus or deficit of requests at a factory. A request is a directed transaction from one factory to another, and not all requests can be fulfilled simultaneously due to the constraints.
Using minimum cost maximum flow might lead to an overcomplication of the problem. In the given problem statement, we don’t have a cost associated with the transactions (requests), so introducing a cost might overcomplicate the model.
The problem can be solved more straightforwardly using depthfirst search (DFS) or bit manipulation. These methods are quite efficient for this problem due to its constraints.
That being said, it’s always excellent to think about different approaches and how established algorithms like MinCost MaxFlow can be applied to new problems. These skills are particularly valuable in competitive programming and algorithm design.
Given the constraints of this problem, a more efficient solution could be obtained by using depthfirst search (DFS) and bit manipulation, which can solve this problem in O(n*2^n). Here’s why:
DepthFirst Search (DFS): DFS is an algorithm for traversing or searching tree or graph data structures. Here, it’s used to check all possible combinations of requests to determine the one with the maximum number of requests that can be fulfilled while maintaining the equilibrium condition at each building. DFS is a perfect choice for this problem because the number of requests is relatively small (up to 16), making it feasible to explore all combinations.
Bit Manipulation: It can be used to represent all subsets of requests, where each bit in an integer represents whether a request is included in the subset or not. This approach can be faster than DFS for iterating through all subsets of requests, especially when the number of requests is not too large.
While network flow models can be powerful tools for solving a variety of optimization problems, for this particular problem, a more direct and efficient approach exists. Considering the constraints of the problem, DFS combined with bit manipulation is more efficient than using a network flow model.
Minimum cost flow worked, you don’t need to add any nodes  just model the cost of a request is 1 and keep canceling negative cycles on the residual graph.
Your approach of using the Minimum Cost Flow (MCF) algorithm also works! It’s great to see a different perspective to solve the problem.
To clarify for those reading, in the context of this problem, the MCF algorithm is used to find the flow through the graph that minimizes the total cost. This is performed by initializing the cost of each request as 1 (indicating the reduction of a request). Then, negative cycles in the residual graph (the graph showing remaining capacities) are cancelled until no such cycles exist.
This approach works by essentially ‘rewarding’ the algorithm for fulfilling a request (since each fulfilled request reduces the total cost by 1). Thus, the MCF algorithm will naturally strive to fulfill as many requests as possible, maximizing the number of fulfilled requests. This way, we obtain our answer as the total flow sent through the network negated, as each unit of flow corresponds to a fulfilled request.
Indeed, the beauty of competitive programming and problemsolving lies in the fact that there can be multiple valid solutions to a problem, each offering its own unique insights. Different algorithms can have different time complexities and are suitable for different situations. The DFS and bit manipulation solution is also a valid approach, and is generally faster and more straightforward for this problem given its constraints. However, it’s always beneficial to explore and understand various ways to approach a problem.
Which approach is efficient for this problem?
Given the constraints of this problem, both the DFS + bit manipulation approach and the Minimum Cost Flow (MCF) approach are viable solutions. However, they offer different tradeoffs in terms of complexity and implementation difficulty.
The DFS + bit manipulation approach can be more efficient in terms of time complexity because it leverages the relatively small input size. This approach essentially tries all combinations of fulfilling the requests (represented by the bits in the mask), which is feasible because the maximum number of requests is limited to 16. The time complexity of this approach is O(n * 2^r), where n is the number of buildings and r is the number of requests. With a maximum of 20 buildings and 16 requests, this approach is computationally feasible.
On the other hand, the MCF approach models the problem as a flow network and uses more sophisticated graph algorithms to solve it. While this approach can handle larger inputs and is a more general solution, it is also more complex to implement and understand.
Therefore, if the goal is to solve this specific problem given its constraints, the DFS + bit manipulation approach may be more suitable due to its efficiency and relative simplicity. However, understanding the MCF approach can be valuable for solving other, more complex problems that involve flow networks.
This question can be solved using bit mask. After a nap, I got an idea from Hamming Weight. HammingWeight(n)=__builtin_popcount(n)
Indeed, the bit mask approach, combined with Hamming Weight (the count of set bits in a binary number), is a great way to solve this problem. Here’s why:
First, we’ll represent each request by a bit in our mask. If the bit is set (1), it means we’re choosing to fulfill that request; if it’s not set (0), we’re not fulfilling that request.
The number of set bits in the bit mask (which is the Hamming Weight) represents the number of requests we’re fulfilling. So, maximizing the Hamming Weight is equivalent to maximizing the number of fulfilled requests.
But we also need to ensure that for each building, the number of employees going out is equal to the number coming in, to keep the building balanced.
So the general approach can be as follows:
Iterate over all possible combinations of requests, represented as bit masks. This can be done by looping from 0 to 2^n  1, where n is the number of requests.
For each combination, we check if it’s valid (i.e., if for each building, the number of employees going out is equal to the number coming in). This can be done by maintaining an array of net transfers for each building, which we update as we iterate over the bits in the mask.
If the combination is valid, we calculate its Hamming Weight (using
__builtin_popcount(n)
in C/C++, for instance). If it’s higher than the maximum we’ve seen so far, we update the maximum.After we’ve checked all combinations, the maximum Hamming Weight is our solution: the maximum number of requests we can fulfill while keeping all buildings balanced.
Note: Although this approach works for this problem due to its small constraints (n <= 20, requests.length <= 16), it wouldn’t be efficient for larger inputs because it has a time complexity of O(n * 2^r), where n is the number of buildings and r is the number of requests.
All you need to do is:
Find all distinct cycles Try every valid combination of cycles (valid means you don’t use the same person twice) Brute force works for both and no need for any caching.
Yes, your approach is correct and it is quite intuitive as well.
The idea is to find all distinct cycles in the directed graph represented by the requests. A cycle here would be a series of requests such that each request’s target is the source of the next request, and the source of the first request is the target of the last request. Each cycle represents a series of requests that can be fulfilled without unbalancing any building, so we want to find as many disjoint cycles as possible.
Once we’ve found all distinct cycles, we can simply use brute force to try every valid combination of these cycles. A combination is valid if no building is used in more than one cycle in the combination. The maximum number of requests we can fulfill is the sum of the lengths of the cycles in the best combination we can find.
As you mentioned, there is no need for any caching or dynamic programming in this approach, as the constraints of the problem (n <= 20, requests.length <= 16) allow brute force to work within reasonable time.
However, it’s worth mentioning that finding all cycles in a directed graph can be a challenging task on its own, and the naive approach of trying all possible combinations of cycles has a time complexity of O(2^c), where c is the number of cycles. So while this approach is conceptually straightforward, implementing it efficiently would require careful thought and planning.
This question is tagged as DP, I’m wondering what would be the DP solution? I’m seeing Backtrack and bitmask so far.
Indeed, this problem is more commonly solved with a backtracking and bitmask approach rather than dynamic programming (DP). The backtracking solution exhaustively checks all combinations of transfer requests to find the maximum number that can be fulfilled, which is feasible given the problem’s constraints.
The bitmask part comes in when representing each set of requests. Each bit in an integer represents whether a particular request is included in the set. This way, generating all subsets of requests becomes equivalent to generating all integers up to 2^n, where n is the number of requests.
While dynamic programming is a powerful technique, applying it to this problem would be challenging. The most obvious states for a DP solution would be the “balance” of each building and the set of requests that have been considered so far. However, there are 2^n possible subsets of requests and each building’s balance can range from n to n, leading to an enormous state space that would be impractical to handle.
Hence, although this problem is tagged as DP on LeetCode, the DP approach may not be the most suitable or efficient for this particular problem. It’s crucial to choose the problemsolving technique that best fits the problem at hand, taking into account the problem’s specific requirements and constraints.
A follow up could be harder, e.g. with larger number of requests and buildings.
Any suggestions on how the solution could be optimized. Sure, we can delete self referential requests and twoway cycles but that is hardly likely to make a big dent on the number of requests remaining if that number is large.
In the case of larger inputs, the exhaustive search approach indeed becomes infeasible due to exponential complexity. To improve the efficiency, we could consider the following techniques:
Pruning: This involves discarding certain choices early in the search if we can prove that they will not lead to an optimal solution. For example, if we have two requests that involve the same employee, and one request has a greater score than the other, we can ignore the lesser request.
Heuristics: These are rules that help in making the search more efficient by prioritizing certain paths. For example, we could prioritize requests that involve employees who appear less frequently in the list of requests.
Parallel processing: If hardware resources allow, we could divide the search space into independent parts and process them in parallel.
Greedy algorithms: These make locally optimal choices at each stage in the hope that these local choices will lead to a global optimum. However, they do not always produce the optimal solution for every problem.
Approximation algorithms: If an exact solution is not feasible in a reasonable time, we can consider algorithms that provide a solution close to the optimal one.
Remember that these techniques depend on the nature of the problem and the specific input data, and their efficiency can vary. Before applying them, it’s necessary to analyze the problem and data characteristics to identify the most promising approaches. However, the problem Maximum Number of Achievable Transfer Requests seems to be a version of the “Maximum Coverage Problem”, which is known to be NPhard, so an exact solution cannot be found in polynomial time. Therefore, in case of larger inputs, we might have to resort to approximation algorithms or heuristics.
Won’t this work Anyone? “Find all cycles and sort them by their length and pick one by one removing those edges from graph”
This approach sounds like a Greedy Algorithm, where you choose the best option (the shortest cycle in this case) at each step. However, it is important to note that Greedy Algorithms do not always guarantee the global optimal solution, and this problem is an example of such a case.
The approach you proposed will find the shortest cycles first and remove those requests from the graph. But choosing the shortest cycles first doesn’t necessarily lead to the maximum number of achievable transfer requests. Consider the case where there are multiple short cycles and one long cycle that includes all the edges of the short cycles. If we choose the short cycles first, we would lose the opportunity to select the long cycle, which could potentially satisfy more transfer requests.
This problem is essentially a variant of the Maximum Coverage Problem, which is NPhard. Therefore, it doesn’t have an efficient solution that works in all cases. The most common approach for this problem is to use a bruteforce method that tries all combinations of requests (or a bit masking technique that achieves the same thing), but this method is not efficient for large inputs. For large inputs, we might need to use approximation algorithms or heuristics, understanding that they might not always provide the optimal solution.
Sounds just like “delete minimum number of edges to make all weak SCCs in directed graph euler”. Does a greedy algorithm for this problem exist? Maybe there is a strategy to pick edges that we remove in euler cycle construction algorithm…
You’re right in recognizing the similarities between this problem and the problem of making all weakly connected components (SCCs) in a directed graph Eulerian by deleting the minimum number of edges. Both involve dealing with directed graphs and have a certain optimization goal.
However, note that making a graph Eulerian involves creating a path or cycle that visits each edge exactly once, which is different from the goal of this problem  to fulfill the maximum number of requests.
Regarding the question of whether a greedy algorithm exists for this problem: Greedy algorithms work well when the problem has the property of “optimal substructure,” meaning that an optimal solution can be constructed efficiently from optimal solutions of its subproblems. Problems that can be solved with greedy algorithms include Kruskalâ€™s Minimum Spanning Tree, Dijkstraâ€™s Shortest Path, etc.
However, this problem doesn’t appear to have an optimal substructure. As mentioned earlier, simply choosing the best local options (like fulfilling the requests that appear most frequently first or choosing the shortest cycles in the graph first) doesn’t necessarily lead to an overall optimal solution. This is why a greedy approach might not work here, and why a bruteforce or bitmasking technique is typically used.
There is no efficient greedy solution has been found for this problem. For larger inputs, approximation algorithms or heuristics might be used, with the understanding that they may not always yield the optimal solution.
Remember that solving such problems often involves a tradeoff between optimality and computational feasibility.
n=3 [[0,0],[1,1],[0,0],[2,0],[2,2],[1,1],[2,1],[0,1],[0,1]] why this input is giving 5 as output ? according to me the output should be 6 please explain?
The problem statement mentions that “Each request is represented by a pair of integers, where the first integer represents the source building and the second one represents the target building.”
Also, keep in mind that buildings have to end with the same number of employees as they started with. So for the request to be fulfilled, the number of outgoing requests (employees leaving) from a building should equal the number of incoming requests (employees entering) to that building.
Let’s go through each request in your example:
 [0, 0]: This request can be ignored since it does not cause any employee to move. So we can fulfill this request.
 [1, 1]: Similar to the first request, this can be ignored since no movement of employees occur. This request can also be fulfilled.
 [0, 0]: This is another selfreferential request and can be fulfilled.
 [2, 0]: An employee moves from building 2 to building 0.
 [2, 2]: This is a selfreferential request and can be fulfilled.
 [1, 1]: This is another selfreferential request and can be fulfilled.
 [2, 1]: An employee moves from building 2 to building 1. Now, building 2 has 0 employees left.
 [0, 1]: An employee moves from building 0 to building 1. Now, building 0 has 0 employees left.
 [0, 1]: We cannot fulfill this request since building 0 has no employees left.
So the maximum number of requests that can be fulfilled is 5. Hence the output is 5, not 6.
The problem aims to find the maximum number of requests that can be simultaneously fulfilled while maintaining a balanced number of requests going in and out of each node.
Why this test case has output is 3 instead of 4? n = 5; requests = [[0,1],[0,1],[1,2],[2,0],[3,4]]; I have think that both two first requests is accepted, but maybe just only one is accepted. Correct me if I am wrong, thanks so much!
The main constraint for this problem is that every building should end up with the same number of employees as it started with. This means that for every employee that leaves a building, an employee must also enter that building.
For the given test case, if we accept both of the first two requests, we will end up with an imbalance in building 0. Two employees will leave building 0, but only one will return (from building 2). So we can’t accept both of the first two requests.
However, we can accept one of the first two requests, along with the third and fourth requests. This gives us the following movement of employees:
 An employee moves from building 0 to building 1.
 An employee moves from building 1 to building 2.
 An employee moves from building 2 to building 0.
Now, buildings 0, 1, and 2 all have the same number of employees as they started with, and we’ve fulfilled 3 requests. We can’t fulfill the fifth request without creating an imbalance, so the maximum number of requests we can fulfill is 3, not 4.
Can anyone tell how there are 4 possible achievable requests in the following testcase? n = 3 requests = [[1,2],[1,2],[2,2],[0,2],[2,1],[1,1],[1,2]]
I’m confused and the only possible transfers I can see here are the selftransfers in a building i.e 2. Can anyone help with this one?
Let’s break down the movements we could make for each request:
 An employee moves from building 1 to building 2.
 An employee moves from building 1 to building 2.
 An employee stays in building 2 (selftransfer).
 An employee moves from building 0 to building 2.
 An employee moves from building 2 to building 1.
 An employee stays in building 1 (selftransfer).
 An employee moves from building 1 to building 2.
If we choose the 1st, 2nd, 5th, and 6th requests, we have the following movements:
 Employee moves from building 1 to building 2.
 Employee moves from building 1 to building 2.
 Employee moves from building 2 to building 1.
 Employee stays in building 1 (selftransfer).
Now, buildings 1 and 2 have the same number of employees as they started with. Building 0 didn’t change, so it’s still balanced. Therefore, we’ve fulfilled 4 requests, which is the maximum for this case.
Let’s make a table to visualize this dataset.
Here, for simplicity and better understanding, we’ll use ‘Initial’ to represent the state of each building before any requests, and ‘Building 0’, ‘Building 1’ and ‘Building 2’ to represent the states after each request. ‘Feasibility’ column indicates whether each request is feasible (i.e., maintains the balance of people in/out of each building).
Request #  Movement  Building 0  Building 1  Building 2  Feasibility 

Initial  0  0  0  N/A  
1  1 > 2  0  1  +1  To be determined 
2  1 > 2  0  2  +2  To be determined 
3  2 > 2  0  2  +2  Disregarded 
4  0 > 2  1  2  +3  To be determined 
5  2 > 1  1  1  +2  To be determined 
6  1 > 1  1  0  +2  Disregarded 
7  1 > 2  1  1  +3  To be determined 
In this table, we can see the impact of each request on the state of each building. However, whether a request is feasible or not depends on the combination of requests chosen. For example, if we select request #1 and request #5, the total effect is 0 for each building, which means these requests are feasible. Therefore, the ‘Feasibility’ column should be filled considering all possible combinations of requests.
The feasibility is calculated based on whether the total number of employees across all buildings remains constant:
Step  Description  Building 0  Building 1  Building 2  Feasibility 

0  Initial  0  0  0  n/a 
1  1st request (1 > 2)  0  1  +1  (0+(1)+1)=0 
2  2nd request (1 > 2)  0  2  +2  (0+(2)+2)=0 
3  5th request (2 > 1)  0  1  +1  (0+(1)+1)=0 
4  6th request (1 > 1)  0  1  +1  (0+(1)+1)=0 
This shows that each step in this sequence of requests keeps the total number of employees constant, making all steps feasible. The feasibility of each step is calculated as the sum of changes in all buildings, and if this sum equals zero, then the step is feasible.
The number of employees staying constant is an invariant in this problem that we need to maintain when we process each request.
That’s correct!
The problem’s constraint is essentially maintaining the invariant that the total number of employees remains constant before and after processing each request.
This is because every request is a transfer of an employee from one building to another, not the creation or removal of employees. Hence, while the number of employees in individual buildings may change as requests are processed, the overall number of employees across all buildings must stay the same.
This constraint plays a vital role in determining the feasibility of a set of requests. For each individual request to be feasible, it must not result in a negative number of employees in any building, and the sum of the employees in all buildings must remain unchanged.
When we’re trying to find the maximum number of achievable requests, we need to consider combinations of requests that respect this invariant. This is key to finding a solution to the problem.
How is this invariant maintained in the code?
In the provided Python code, the invariant that the total number of employees remains constant is maintained in the recursion.
Here’s a brief breakdown of how it’s done:
The helper function
dfs(idx, outdegree, indegree)
is used to explore all possible combinations of requests. Here,idx
is the index of the current request we are considering,outdegree
is an array storing the number of employees that need to leave each building, andindegree
is an array storing the number of employees that need to arrive at each building.For each request, there are two possibilities: either we take this request, or we skip it. If we take the request, we need to update the
outdegree
andindegree
arrays to reflect this transfer of an employee.After considering a request, the function calls itself recursively to consider the next request, and the
outdegree
andindegree
are updated back after the recursive call.If we have processed all requests, we check if for every building, the number of employees leaving is equal to the number of employees arriving. If so, the set of requests is valid and we return the number of taken requests.
In this way, by carefully tracking the number of employees leaving and arriving at each building, and by checking the condition at the end, the code ensures the invariant that the total number of employees remains constant is always maintained.
The problem aims to find the maximum number of requests that can be simultaneously fulfilled while maintaining a balanced number of requests going in and out of each node.
You are correct. The main objective of this problem is to maximize the number of transfer requests that can be fulfilled while keeping the number of employees in each building constant. In other words, for each building, the number of employees leaving must be equal to the number of employees arriving.
This can be visualized as a directed graph where each node (building) needs to maintain the same number of incoming and outgoing edges (transfers). The maximum number of achievable transfer requests is thus the maximum number of edges in this graph that can form cycles, as each cycle represents a set of transfer requests that can be completed while keeping the total number of employees in each building constant.
Various algorithms and techniques like DepthFirst Search (DFS), BreadthFirst Search (BFS), Dynamic Programming (DP), bitmasking, or network flow could be potentially used to solve this problem, depending on the specific constraints and requirements.
You are correct. The main objective of this problem is to maximize the number of transfer requests that can be fulfilled while keeping the number of employees in each building constant. In other words, for each building, the number of employees leaving must be equal to the number of employees arriving.
This can be visualized as a directed graph where each node (building) needs to maintain the same number of incoming and outgoing edges (transfers). The maximum number of achievable transfer requests is thus the maximum number of edges in this graph that can form cycles, as each cycle represents a set of transfer requests that can be completed while keeping the total number of employees in each building constant.
Various algorithms and techniques like DepthFirst Search (DFS), BreadthFirst Search (BFS), Dynamic Programming (DP), bitmasking, or network flow could be potentially used to solve this problem, depending on the specific constraints and requirements.
I came with this solution, but it fails as I’m not comparing in which choice we can get maximum achievable requests. 106/117 test cases passed.
can someone plz help in modifying this logic to work?


The problem you’re trying to solve requires you to consider all possible combinations of fulfilling the transfer requests, and to find the maximum number of transfer requests that can be fulfilled without violating the constraints of the problem. Therefore, this problem requires a solution that searches the entire space of possible combinations, such as a depthfirst search (DFS) or breadthfirst search (BFS) with backtracking, to find the maximum number of requests that can be fulfilled.
Your current implementation does a depthfirst search through the transfer requests, but it appears to be missing the component where it checks all possible combinations. The function dfs
seems to try to fulfill every transfer request it encounters without considering whether fulfilling a particular request is beneficial or not. This could lead to situations where fulfilling a request leads to fewer overall fulfillments, but because the request is fulfilled without consideration, it’s counted towards the final result.
In addition, you’re using a variable achiReq
to keep track of the maximum number of achievable requests, but it’s not clear how this variable is updated correctly during the search. You might want to consider moving this variable inside the dfs
function and updating it as part of the recursive call.
One way to address these issues is to adjust your DFS to a backtracking approach:
Iterate through all possible combinations of requests. You can do this by creating a recursive function that considers both fulfilling and not fulfilling the current request.
For each combination, calculate the number of requests that can be fulfilled (i.e., where the total number of employees in each building remains constant), and keep track of the maximum number of fulfillable requests.
Return the maximum number of fulfillable requests.
Here is a Python code snippet to illustrate this approach:


This code starts with an empty set of fulfilled requests and tries to fulfill each request one by one. If fulfilling a request doesn’t violate the conditions of the problem, it fulfills the request and continues with the next request. After trying both fulfilling and not fulfilling the request, it “backtracks” and returns to the previous state.
Please note that the code is in Python, but the main idea can be implemented in other languages as well. You can try to modify your Java code accordingly.
The Intuition is to use backtracking approach to explore all possible combinations of taking or not taking transfer requests. It maintains a count of transfer requests and checks if the requests are balanced for each building. The maximum count of transfer requests that satisfies the balanced request condition is returned as the result.
Yes, you are right. To make it a bit clearer, let’s break it down:
All possible combinations: You will want to explore all the possible combinations of requests that can be made. This can be represented as a binary string where the ith bit represents whether or not to take the ith request. There will be 2^n combinations of requests.
Backtracking: This is a common technique used in problems like this where you want to explore all possible combinations. It’s a form of depthfirst search (DFS). You go down each path until you reach a deadend, and then you backtrack and try a different path. In this case, you’re trying all combinations of taking and not taking each request.
Counting requests and checking for balance: For each combination of requests, you’re keeping track of the number of requests you’ve fulfilled, as well as whether the requests are balanced for each building. The balance is represented by the
balance
array in the Python code, wherebalance[i]
is the number of people moving into buildingi
minus the number of people moving out of buildingi
. Ifbalance[i]
is zero for alli
, that means the requests are balanced.Maximum count: You’re also keeping track of the maximum count of transfer requests that satisfy the balance condition. This is represented by the
self.res
variable in the Python code. This is updated whenever a new combination of requests is found that fulfills more requests than the current maximum and also satisfies the balance condition.
Hope this explanation clarifies your understanding!
Guys I think you could do it the “not take” first and “take” next way. Please correct me if I’m wrong…
helper(start + 1,requests, indegree, n, count);
indegree[requests[start][0]];
indegree[requests[start][1]]++;
helper(start + 1,requests, indegree, n, count + 1);
indegree[requests[start][0]]++;
indegree[requests[start][1]];
Yes, you’re correct.
The idea behind this is to use a depthfirst search (DFS) approach with backtracking, which means we first explore the path of not taking a request, and then explore the path of taking the request.
Here is what happens in your provided code:
helper(start + 1, requests, indegree, n, count);
: This line explores the path where thestart
th request is not taken. We move on to the next request by incrementingstart
, while keepingcount
andindegree
(which represents the balance of employees in each building) unchanged.indegree[requests[start][0]]; indegree[requests[start][1]]++;
: Now we decide to take thestart
th request. This involves decreasing the balance of the source building and increasing the balance of the target building.helper(start + 1, requests, indegree, n, count + 1);
: We then explore the remaining requests with thestart
th request taken. Notice thatcount
is incremented by 1 to reflect that we’ve taken an additional request.indegree[requests[start][0]]++; indegree[requests[start][1]];
: This is the backtracking part. After exploring all paths with thestart
th request taken, we need to revert the changes we’ve made before moving on to the next request. This ensures that the decision of taking thestart
th request does not affect the exploration of later requests.
So yes, your logic is correct. Doing the “not take” first and “take” next gives you all combinations of requests and allows you to find the maximum number of achievable requests.
how did you figure out that it can be done by recursion i was thinking of storing indegree and outdegreee but then cant quite think what to do with it
The problem can be broken down into a number of decisions, where each decision involves whether to fulfill a transfer request or not. Since there are two options for each decision, this naturally leads to a binary tree structure of possibilities. This is why a recursive approach, particularly a depthfirst search (DFS) approach, fits this problem well.
In each recursive call, we decide whether to take the current request or not. If we take it, we adjust the indegree and outdegree accordingly. Then we move on to the next request.
The indegree and outdegree (or simply, balance of employees) of each building is crucial because we need to ensure that the total number of employees in each building remains constant. Therefore, we need to keep track of this information and pass it to each recursive call.
Ultimately, by exploring all possibilities, we can find the maximum number of requests that can be fulfilled while maintaining the invariant that each building’s total number of employees remains constant.
all the edges that can be a part of cycles in a graph . is this approach correct?
Your approach of considering edges that are part of a cycle in a graph is indeed a starting point for this problem, but it isn’t enough on its own. The reason is that not all cycles are valid sequences of transfers that would satisfy the problem’s constraints. Moreover, simply having an edge be part of a cycle doesn’t mean that it will be involved in the optimal solution.
The challenge of this problem lies in determining the specific sequences of transfers that would both maximize the number of fulfilled requests and satisfy the condition that the total number of employees in each building remains constant. Therefore, while identifying cycles can help identify potential transfer sequences, the actual task involves making a series of decisions about whether to include each request in the solution or not.
This is why a more comprehensive approach like depthfirst search (DFS) or dynamic programming (DP) is usually used to solve this problem. By systematically exploring all potential combinations of requests, these approaches can find the optimal solution.
So, to summarize, while considering cycles can be a part of the solution strategy, a more complete search approach that considers all possible combinations of requests is required to correctly solve this problem.
In Java solution replace helper(start + 1,requests, indegree, n, count); with if( requests[start][0] != requests[start][1] ) { helper(start + 1,requests, indegree, n, count); } as if transfer request is for same building then there’s no need to skip that request as it’ll always be counted in the max number of requests. This will make your solution beat 100%
That’s a very smart optimization. Since transfer requests for the same building will always be fulfilled (as it doesn’t violate the condition that the total number of employees in each building remains constant), we can indeed bypass checking the ’nottake’ scenario for these requests in the recursion.
Here’s how your improved Java solution might look:


This solution is checking each combination of request (either taking or not taking each request). However, if the request is for the same building, we don’t need to check the ’nottake’ scenario, as that request will always be counted in the max number of requests.
This solution should be more efficient than the previous one and also be able to solve all the test cases correctly. The key insight is that for every node (building), the total number of employees moving in should equal the total number of employees moving out for the building status to be balanced.
why did we return after we encounter a value in the indegree vector not equal to 0?
The goal of the problem is to fulfill as many requests as possible, while maintaining the condition that the number of employees in each building remains constant (i.e., for each building, the number of employees moving in equals the number of employees moving out).
When we traverse through the ‘balance’ array, if we find any building (or node) where the ‘balance’ value is not 0, it means that for this building, the number of employees moving in is not equal to the number of employees moving out. This breaks the constant number of employees condition mentioned in the problem, so we cannot consider this sequence of requests valid.
As a result, we terminate the current branch of our recursion and return. We’re not considering this sequence any further because it’s invalid and cannot be part of our solution. The goal is to search for a valid sequence that fulfills the maximum number of requests, so any invalid sequence can be disregarded.
This is part of the reason why we use backtracking for this problem: it allows us to explore different combinations of requests and quickly discard those that are invalid, which helps improve the efficiency of our solution.
Hey ! I am just confused with the question . “a list of requests is achievable only if for each building, the net change in employee transfers is zero” so considering this for the first example there are 3 buildings which satisfies this condition .isn’t the answer supposed to be 3? & why are we trying take & not take the request’s i don’t get it aren’t we supposed to calculate for all the buildings and find how many satisfies the condition!!!!!
It seems like there might be a bit of confusion about the statement “a list of requests is achievable only if for each building, the net change in employee transfers is zero.”
This statement is about the overall effect of a list of requests. For a list of requests to be “achievable,” it must be the case that for every building, the number of employees that want to leave that building equals the number of employees that want to enter that building. If this condition is not met, then it would not be possible to satisfy all requests in the list, because some building would either end up with too many or too few employees.
In other words, this condition is not about the number of buildings that satisfy this condition, but rather about the overall system of requests. That’s why we can’t simply count the number of buildings that satisfy the condition.
To determine the maximum number of achievable requests, the problem is approached by backtracking and exploring all possible combinations of requests. For each request, we have two options: take it (fulfill the request) or not take it (ignore the request). By exploring all combinations, we can find the one that maximizes the number of fulfilled requests while keeping each building’s inandout balance at zero.
Let’s consider the first example:
n = 5 requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
The maximum number of requests we can achieve while keeping inandout balance at zero for each building is 5, and one possible sequence to achieve this is as follows:
 Request 1: Employee moves from building 0 to building 1.
 Request 2: Employee moves from building 1 to building 0.
 Request 3: Employee moves from building 0 to building 1.
 Request 4: Employee moves from building 1 to building 2.
 Request 5: Employee moves from building 2 to building 0.
As you can see, each building has an equal number of incoming and outgoing transfers, hence maintaining the balance. The fifth request (from building 3 to building 4) is ignored in this case to maximize the total number of fulfilled requests while still maintaining the balance at each building.
here the main moto is to find the maximum number of request achievable. A single building might have multiple request. Say we have n=1 i.e. only one building but the request array is [[0,0],[0,0],[0,0],[0,0]] so here we see that each request signifies each employee’s transfer request. So we can TRANSFER hence satisfy 4 requests AT MAXIMUM.
Yes, you’re correct. In the context of this problem, each request in the array represents a transfer request made by an individual employee. If the start and end building of a transfer request are the same (like [0,0]), it means the employee wishes to stay in the current building, i.e., they’re transferring to the same building they’re currently in. This is also known as a selftransfer.
So in your example where n=1 (indicating there’s only one building) and the request array is [[0,0],[0,0],[0,0],[0,0]], we have four employees all requesting to stay in building 0. Since there’s only one building, they’re essentially already in the building they wish to transfer to. Therefore, we can satisfy all four requests, so the maximum number of achievable requests would be 4.
This scenario underlines the fact that the problem is not just about keeping the net change of employees in each building at zero, but also about fulfilling as many individual transfer requests as possible. In scenarios where an employee wishes to stay in the same building, we can always fulfill their request, as it doesn’t affect the total number of employees in any building.
Case Analysis
Let’s consider the following cases one by one:
 n = 0, requests = []
In this case, there are no buildings and no requests. Since no requests need to be fulfilled, we can say that all requests are already fulfilled. Therefore, the maximum number of achievable requests is 0.
Now consider n = 1 requests array with 1 transfer. Then consider n = 2 with requests array with two transfer.
Scenario 1: n = 1, requests = [[0,0]]
In this scenario, there is only one building and one transfer request. The transfer request is a selftransfer, meaning the employee requests to stay in the current building, which is always achievable. Therefore, the maximum number of achievable requests is 1.
Scenario 2: n = 2, requests = [[0,1], [1,0]]
In this scenario, there are two buildings and two transfer requests. The first request is for an employee to move from building 0 to building 1, and the second request is for an employee to move from building 1 to building 0. These two requests can be fulfilled simultaneously since an employee leaving a building is replaced by an incoming employee. Hence, the maximum number of achievable requests is 2.
In both of these cases, the net change in employee transfers for each building is zero, which is a condition for the request list to be achievable.
There are multiple types of scenarios that can arise with different combinations of transfer requests. Here are few types:
SelfTransfer: This is when an employee requests to stay in their current building. These requests are always achievable as they do not affect the distribution of employees across the buildings. An example is [[0,0]] for a single building scenario or [[0,0],[1,1],[2,2]] for multiple buildings.
Swap: This is when two employees from different buildings request to transfer to each other’s building. This is also always achievable as the net change in employee transfers for each building remains zero. An example is [[0,1],[1,0]].
Cycle: This is when more than two employees request to transfer to different buildings in a way that forms a cycle. For example, an employee from building 0 wants to move to building 1, an employee from building 1 wants to move to building 2, and an employee from building 2 wants to move to building 0. This is achievable because the net change in employee transfers for each building remains zero. An example is [[0,1],[1,2],[2,0]].
Partial Cycle/Swap with SelfTransfer: This is a combination of swap or cycle with selftransfer. For example, two employees swap their buildings, and another employee decides to stay. This is also achievable as it maintains the overall balance. An example is [[0,1],[1,0],[2,2]].
Unachievable Requests: These are situations where it’s impossible to fulfill all requests while maintaining the balance of employees in all buildings. For example, if two employees from the same building want to move to another building, but no employees from the other building want to move to this building, these requests are unachievable. An example is [[0,1],[0,1]]. In such scenarios, we need to find the maximum number of achievable requests.
Remember that the objective is to find the maximum number of requests that can be achieved while maintaining the balance of employees in all buildings.
Similar Problems
Here are 10 LeetCode problems that are similar to the problem “Maximum Number of Achievable Transfer Requests” in terms of problemsolving approaches or involving similar concepts.
LeetCode 90: Subsets II  This problem also involves using bit manipulation to generate all possible subsets, similar to how we generate all possible combinations of requests in our problem.
LeetCode 207: Course Schedule  This problem involves a similar graph traversal mechanism as our problem, and we need to detect cycles, which is similar to how we detect cycles of requests in our problem.
LeetCode 332: Reconstruct Itinerary  This problem also involves a similar graphbased approach and a depthfirst search (DFS) for finding an itinerary that fulfills certain conditions, like our problem where we are finding a sequence of requests that fulfills certain conditions.
LeetCode 212: Word Search II  The approach for this problem also uses DFS and backtracking which is similar to the given problem.
LeetCode 980: Unique Paths III  This problem involves finding all unique paths from the start cell to the end cell on a grid, which is similar to finding all possible request sequences in our problem.
LeetCode 200: Number of Islands  The problem requires a DFS search in a 2D grid, similar to how we traverse a graph in our problem.
LeetCode 46: Permutations  This problem is about finding all permutations of a list, similar to how we try to find all combinations of requests in our problem.
LeetCode 78: Subsets  The problem involves generating all possible subsets, which is similar to generating all possible combinations of requests in our problem.
LeetCode 934: Shortest Bridge  The problem requires finding the shortest path to connect two islands, similar to the concept of finding cycles of requests in our problem.
LeetCode 126: Word Ladder II  This problem involves finding all shortest transformation sequences from a begin word to an end word, which is similar to our problem where we need to find all possible sequences of requests that fulfill certain conditions.
Remember, although these problems may involve similar concepts or problemsolving approaches, each problem has its own unique aspects and may require modifications or additions to these approaches. It’s also important to analyze the problem carefully and come up with a solution approach that fits the problem’s requirements and constraints.