1601. Maximum Number of Achievable Transfer Requests

阅读: 评论:0

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:

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

思路:

题目说了长度只有16,那么用dfs暴力搜索即可。另外用一个成员变量记录最大值,之后每次递归都是选或者不选。

代码:

class Solution {
public:
    int maximumRequests(int n, vector<vector<int>>& requests) {
        vector<int> count(n, 0);
        check(requests, count, 0, 0);
        return ans;
    }
private:
    int ans = 0;
    void check(vector<vector<int>>& requests, vector<int>& count, int index, int cur) {
        if (cur + requests.size() - index < ans) {
            return;
        }
        if (index == requests.size()) {
            for (auto i : count) {
                if (i != 0)
                    return;
            }
            ans = max(ans, cur);
            return;
        }
        count[requests[index][0]]--;
        count[requests[index][1]]++;
        check(requests, count, index + 1, cur + 1);
        count[requests[index][0]]++;
        count[requests[index][1]]--;
        check(requests, count, index + 1, cur);
    }
};

本文发布于:2024-02-04 07:35:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170702132753584.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Number   Maximum   Requests   Transfer   Achievable
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23