leetcode - 1601. Maximum Number of Achievable Transfer Requests

Description

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

Solution

Backtrace, use 0 and 1 to denote whether we approve the current request or not.

Time complexity: o ( 2 M ∗ N ) o(2^M*N) o(2MN), M is the number of requests, N is the number of buildings.
Space complexity: o ( N + M ) o(N+M) o(N+M)

Code

class Solution:
    def __init__(self,) -> None:
        self.memo = {}

    def helper(self, requests: List[List[int]], buildings: dict, apv_requests: int, requests_tag: str) -> int:
        """
        Args:
            requests:
            buildings: {b1: 0, b2: 0, ...}
            apv_requests: int
            requests_tag: '00001', 0 for not use, 1 for use
        Returns:
            maximum number of approved requests
        """
        if requests_tag in self.memo:
            return self.memo[requests_tag]
        res = 0
        for index, each_request in enumerate(requests):
            # approve current request
            buildings[each_request[0]] -= 1
            buildings[each_request[1]] += 1
            res = max(res, self.helper(requests[index + 1:], buildings, apv_requests + 1, requests_tag + '1'))
            buildings[each_request[0]] += 1
            buildings[each_request[1]] -= 1
            # disapprove current request
            res = max(res, self.helper(requests[index + 1:], buildings, apv_requests, requests_tag + '0'))
        if all(item == 0 for item in buildings.values()):
            res = max(res, apv_requests)
        self.memo[requests_tag] = res
        return res
        
    def maximumRequests(self, n: int, requests: List[List[int]]) -> int:
        return self.helper(requests, {i: 0 for i in range(n)}, 0, '')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值