Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.
You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.
All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2, there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2.
Return the maximum number of achievable requests.
Example 1:
Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]] Output: 5 Explantion: Let's see the requests: From building 0 we have employees x and y and both want to move to building 1. From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively. From building 2 we have employee z and they want to move to building 0. From building 3 we have employee c and they want to move to building 4. From building 4 we don't have any requests. We can achieve the requests of users x and b by swapping their places. We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
Example 2:
Input: n = 3, requests = [[0,0],[1,2],[2,1]] Output: 3 Explantion: Let's see the requests: From building 0 we have employee x and they want to stay in the same building 0. From building 1 we have employee y and they want to move to building 2. From building 2 we have employee z and they want to move to building 1. We can achieve all the requests.
Example 3:
Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]] Output: 4
Constraints:
1 <= n <= 201 <= requests.length <= 16requests[i].length == 20 <= fromi, toi < nProblem summary: We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in. You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi. All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2, there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2. Return the maximum number of achievable requests.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Backtracking · Bit Manipulation
5 [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
3 [[0,0],[1,2],[2,1]]
4 [[0,3],[3,1],[1,2],[2,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1601: Maximum Number of Achievable Transfer Requests
class Solution {
private int m;
private int n;
private int[][] requests;
public int maximumRequests(int n, int[][] requests) {
m = requests.length;
this.n = n;
this.requests = requests;
int ans = 0;
for (int mask = 0; mask < 1 << m; ++mask) {
int cnt = Integer.bitCount(mask);
if (ans < cnt && check(mask)) {
ans = cnt;
}
}
return ans;
}
private boolean check(int mask) {
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
if ((mask >> i & 1) == 1) {
int f = requests[i][0], t = requests[i][1];
--cnt[f];
++cnt[t];
}
}
for (int v : cnt) {
if (v != 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #1601: Maximum Number of Achievable Transfer Requests
func maximumRequests(n int, requests [][]int) (ans int) {
m := len(requests)
check := func(mask int) bool {
cnt := make([]int, n)
for i, r := range requests {
if mask>>i&1 == 1 {
f, t := r[0], r[1]
cnt[f]--
cnt[t]++
}
}
for _, v := range cnt {
if v != 0 {
return false
}
}
return true
}
for mask := 0; mask < 1<<m; mask++ {
cnt := bits.OnesCount(uint(mask))
if ans < cnt && check(mask) {
ans = cnt
}
}
return
}
# Accepted solution for LeetCode #1601: Maximum Number of Achievable Transfer Requests
class Solution:
def maximumRequests(self, n: int, requests: List[List[int]]) -> int:
def check(mask: int) -> bool:
cnt = [0] * n
for i, (f, t) in enumerate(requests):
if mask >> i & 1:
cnt[f] -= 1
cnt[t] += 1
return all(v == 0 for v in cnt)
ans = 0
for mask in range(1 << len(requests)):
cnt = mask.bit_count()
if ans < cnt and check(mask):
ans = cnt
return ans
// Accepted solution for LeetCode #1601: Maximum Number of Achievable Transfer Requests
/**
* [1601] Maximum Number of Achievable Transfer Requests
*
* We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.
* You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.
* All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2, there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2.
* Return the maximum number of achievable requests.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/move1.jpg" style="width: 600px; height: 406px;" />
* Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
* Output: 5
* Explantion: Let's see the requests:
* From building 0 we have employees x and y and both want to move to building 1.
* From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.
* From building 2 we have employee z and they want to move to building 0.
* From building 3 we have employee c and they want to move to building 4.
* From building 4 we don't have any requests.
* We can achieve the requests of users x and b by swapping their places.
* We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/move2.jpg" style="width: 450px; height: 327px;" />
* Input: n = 3, requests = [[0,0],[1,2],[2,1]]
* Output: 3
* Explantion: Let's see the requests:
* From building 0 we have employee x and they want to stay in the same building 0.
* From building 1 we have employee y and they want to move to building 2.
* From building 2 we have employee z and they want to move to building 1.
* We can achieve all the requests.
* Example 3:
*
* Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
* Output: 4
*
*
* Constraints:
*
* 1 <= n <= 20
* 1 <= requests.length <= 16
* requests[i].length == 2
* 0 <= fromi, toi < n
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/
// discuss: https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_requests(n: i32, requests: Vec<Vec<i32>>) -> i32 {
let mut dp = vec![0; n as usize];
let i = 0;
Self::dfs_helper(&mut dp, n as usize, &requests, i)
}
fn dfs_helper(dp: &mut Vec<i32>, _n: usize, requests: &Vec<Vec<i32>>, i: usize) -> i32 {
if i == requests.len() {
return if dp.iter().all(|&x| x == 0) {
0
} else {
std::i32::MIN
};
}
dp[requests[i][0] as usize] -= 1;
dp[requests[i][1] as usize] += 1;
let take = 1 + Self::dfs_helper(dp, _n, requests, i + 1);
dp[requests[i][0] as usize] += 1;
dp[requests[i][1] as usize] -= 1;
take.max(Self::dfs_helper(dp, _n, requests, i + 1))
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1601_example_1() {
let n = 5;
let requests = vec![
vec![0, 1],
vec![1, 0],
vec![0, 1],
vec![1, 2],
vec![2, 0],
vec![3, 4],
];
let result = 5;
assert_eq!(Solution::maximum_requests(n, requests), result);
}
#[test]
fn test_1601_example_2() {
let n = 3;
let requests = vec![vec![0, 0], vec![1, 2], vec![2, 1]];
let result = 3;
assert_eq!(Solution::maximum_requests(n, requests), result);
}
#[test]
fn test_1601_example_3() {
let n = 5;
let requests = vec![vec![0, 3], vec![3, 1], vec![1, 2], vec![2, 0]];
let result = 4;
assert_eq!(Solution::maximum_requests(n, requests), result);
}
}
// Accepted solution for LeetCode #1601: Maximum Number of Achievable Transfer Requests
function maximumRequests(n: number, requests: number[][]): number {
const m = requests.length;
let ans = 0;
const check = (mask: number): boolean => {
const cnt = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
if ((mask >> i) & 1) {
const [f, t] = requests[i];
--cnt[f];
++cnt[t];
}
}
return cnt.every(v => v === 0);
};
for (let mask = 0; mask < 1 << m; ++mask) {
const cnt = bitCount(mask);
if (ans < cnt && check(mask)) {
ans = cnt;
}
}
return ans;
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.