[LeetCode] 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:

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 <= 20
  • 1 <= requests.length <= 16
  • requests[i].length == 2
  • 0 <= fromi, toi < n

最多可达成的换楼请求数目。

我们有 n 栋楼,编号从 0 到 n - 1 。每栋楼有若干员工。由于现在是换楼的季节,部分员工想要换一栋楼居住。

给你一个数组 requests ,其中 requests[i] = [fromi, toi] ,表示一个员工请求从编号为 fromi 的楼搬到编号为 toi 的楼。

一开始 所有楼都是满的,所以从请求列表中选出的若干个请求是可行的需要满足 每栋楼员工净变化为 0 。意思是每栋楼 离开 的员工数目 等于 该楼 搬入 的员工数数目。比方说 n = 3 且两个员工要离开楼 0 ,一个员工要离开楼 1 ,一个员工要离开楼 2 ,如果该请求列表可行,应该要有两个员工搬入楼 0 ,一个员工搬入楼 1 ,一个员工搬入楼 2 。

请你从原请求列表中选出若干个请求,使得它们是一个可行的请求列表,并返回所有可行列表中最大请求数目。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-number-of-achievable-transfer-requests
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我这里暂时给出回溯backtracking的解法,别的更精妙的解法日后我自己想通了再来补充。这里我们需要一个helper函数帮助回溯。helper函数包含一个长度为 N 的count数组以记录人员的进出情况。遍历requests数组,回溯结束的条件可以设置成当requests数组遍历完的时候,这个时候需要查看count数组,如果count数组里面有任何一个位置上不为0,则说明失败了,净变化不为0,直接return。

在遍历requests的时候,你既可以选择满足当前某个request也可以选择不满足(跳过),所以这个helper函数的写法还是跟一般回溯题目的写法稍有不同的。如果选择满足当前request,回溯的时候需要记得把状态还原;如果选择不满足当前request,只需要index往前走即可。

时间O(2^16)

空间O(n)

Java实现

 1 class Solution {
 2     int max = 0;
 3 
 4     public int maximumRequests(int n, int[][] requests) {
 5         helper(requests, 0, new int[n], 0);
 6         return max;
 7     }
 8 
 9     private void helper(int[][] requests, int index, int[] count, int num) {
10         if (index == requests.length) {
11             for (int i : count) {
12                 if (i != 0) {
13                     return;
14                 }
15             }
16             max = Math.max(max, num);
17             return;
18         }
19         // choose
20         count[requests[index][0]]++;
21         count[requests[index][1]]--;
22         helper(requests, index + 1, count, num + 1);
23         count[requests[index][0]]--;
24         count[requests[index][1]]++;
25         // not choose
26         helper(requests, index + 1, count, num);
27     }
28 }

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/13743302.html