Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1601: Maximum Number of Achievable Transfer Requests

In this guide, we solve Leetcode #1601 Maximum Number of Achievable Transfer Requests in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

We have n buildings numbered from 0 to n - 1. Each building has a number of employees.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Bit Manipulation, Array, Backtracking, Enumeration

Intuition

We must explore combinations of choices, but many branches can be pruned early.

Backtracking enumerates valid candidates while keeping the search space under control.

Approach

Use DFS to build candidates step by step, and backtrack when constraints are violated.

Pruning keeps the exploration practical for typical constraints.

Steps:

  • Define the decision tree.
  • DFS through choices and backtrack.
  • Prune invalid paths early.

Example

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.

Python Solution

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

Complexity

The time complexity is O(2m×(m+n))O(2^m \times (m + n))O(2m×(m+n)), and the space complexity is O(n)O(n)O(n), where mmm and nnn are the lengths of the room change request list and the number of rooms, respectively. The space complexity is O(n)O(n)O(n), where mmm and nnn are the lengths of the room change request list and the number of rooms, respectively.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy