java.lang.Object
g1601_1700.s1601_maximum_number_of_achievable_transfer_requests.Solution

public class Solution extends Object
1601 - Maximum Number of Achievable Transfer Requests.<p>Hard</p> <p>We have <code>n</code> buildings numbered from <code>0</code> to <code>n - 1</code>. Each building has a number of employees. It&rsquo;s transfer season, and some employees want to change the building they reside in.</p> <p>You are given an array <code>requests</code> where <code>requests[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represents an employee&rsquo;s request to transfer from building <code>from<sub>i</sub></code> to building <code>to<sub>i</sub></code>.</p> <p><strong>All buildings are full</strong> , so a list of requests is achievable only if for each building, the <strong>net change in employee transfers is zero</strong>. This means the number of employees <strong>leaving</strong> is <strong>equal</strong> to the number of employees <strong>moving in</strong>. For example if <code>n = 3</code> and two employees are leaving building <code>0</code>, one is leaving building <code>1</code>, and one is leaving building <code>2</code>, there should be two employees moving to building <code>0</code>, one employee moving to building <code>1</code>, and one employee moving to building <code>2</code>.</p> <p>Return <em>the maximum number of achievable requests</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/09/10/move1.jpg" alt="" /></p> <p><strong>Input:</strong> n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]</p> <p><strong>Output:</strong> 5 <strong>Explantion:</strong> Let&rsquo;s see the requests:</p> <p>From building 0 we have employees x and y and both want to move to building 1.</p> <p>From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.</p> <p>From building 2 we have employee z and they want to move to building 0.</p> <p>From building 3 we have employee c and they want to move to building 4.</p> <p>From building 4 we don&rsquo;t have any requests.</p> <p>We can achieve the requests of users x and b by swapping their places.</p> <p>We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/09/10/move2.jpg" alt="" /></p> <p><strong>Input:</strong> n = 3, requests = [[0,0],[1,2],[2,1]]</p> <p><strong>Output:</strong> 3 <strong>Explantion:</strong> Let&rsquo;s see the requests:</p> <p>From building 0 we have employee x and they want to stay in the same building 0.</p> <p>From building 1 we have employee y and they want to move to building 2.</p> <p>From building 2 we have employee z and they want to move to building 1.</p> <p>We can achieve all the requests.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]</p> <p><strong>Output:</strong> 4</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 20</code></li> <li><code>1 <= requests.length <= 16</code></li> <li><code>requests[i].length == 2</code></li> <li><code>0 <= from<sub>i</sub>, to<sub>i</sub> < n</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumRequests

      public int maximumRequests(int n, int[][] requests)