Skip to content

Commit faa5f50

Browse files
committed
1601. Maximum Number of Achievable Transfer Requests: AC
1 parent 906583a commit faa5f50

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1207,3 +1207,4 @@ mod s1595_minimum_cost_to_connect_two_groups_of_points;
12071207
mod s1598_crawler_log_folder;
12081208
mod s1599_maximum_profit_of_operating_a_centennial_wheel;
12091209
mod s1600_throne_inheritance;
1210+
mod s1601_maximum_number_of_achievable_transfer_requests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* [1601] Maximum Number of Achievable Transfer Requests
3+
*
4+
* 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.
5+
* You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.
6+
* 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.
7+
* Return the maximum number of achievable requests.
8+
*
9+
* Example 1:
10+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/move1.jpg" style="width: 600px; height: 406px;" />
11+
* Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
12+
* Output: 5
13+
* Explantion: Let's see the requests:
14+
* From building 0 we have employees x and y and both want to move to building 1.
15+
* From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.
16+
* From building 2 we have employee z and they want to move to building 0.
17+
* From building 3 we have employee c and they want to move to building 4.
18+
* From building 4 we don't have any requests.
19+
* We can achieve the requests of users x and b by swapping their places.
20+
* We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
21+
*
22+
* Example 2:
23+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/move2.jpg" style="width: 450px; height: 327px;" />
24+
* Input: n = 3, requests = [[0,0],[1,2],[2,1]]
25+
* Output: 3
26+
* Explantion: Let's see the requests:
27+
* From building 0 we have employee x and they want to stay in the same building 0.
28+
* From building 1 we have employee y and they want to move to building 2.
29+
* From building 2 we have employee z and they want to move to building 1.
30+
* We can achieve all the requests.
31+
* Example 3:
32+
*
33+
* Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
34+
* Output: 4
35+
*
36+
*
37+
* Constraints:
38+
*
39+
* 1 <= n <= 20
40+
* 1 <= requests.length <= 16
41+
* requests[i].length == 2
42+
* 0 <= fromi, toi < n
43+
*
44+
*/
45+
pub struct Solution {}
46+
47+
// problem: https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/
48+
// discuss: https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/discuss/?currentPage=1&orderBy=most_votes&query=
49+
50+
// submission codes start here
51+
52+
impl Solution {
53+
pub fn maximum_requests(n: i32, requests: Vec<Vec<i32>>) -> i32 {
54+
let mut dp = vec![0; n as usize];
55+
let i = 0;
56+
Self::dfs_helper(&mut dp, n as usize, &requests, i)
57+
}
58+
59+
fn dfs_helper(dp: &mut Vec<i32>, _n: usize, requests: &Vec<Vec<i32>>, i: usize) -> i32 {
60+
if i == requests.len() {
61+
return if dp.iter().all(|&x| x == 0) {
62+
0
63+
} else {
64+
std::i32::MIN
65+
};
66+
}
67+
dp[requests[i][0] as usize] -= 1;
68+
dp[requests[i][1] as usize] += 1;
69+
let take = 1 + Self::dfs_helper(dp, _n, requests, i + 1);
70+
dp[requests[i][0] as usize] += 1;
71+
dp[requests[i][1] as usize] -= 1;
72+
take.max(Self::dfs_helper(dp, _n, requests, i + 1))
73+
}
74+
}
75+
76+
// submission codes end
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::*;
81+
82+
#[test]
83+
fn test_1601_example_1() {
84+
let n = 5;
85+
let requests = vec![
86+
vec![0, 1],
87+
vec![1, 0],
88+
vec![0, 1],
89+
vec![1, 2],
90+
vec![2, 0],
91+
vec![3, 4],
92+
];
93+
94+
let result = 5;
95+
96+
assert_eq!(Solution::maximum_requests(n, requests), result);
97+
}
98+
99+
#[test]
100+
fn test_1601_example_2() {
101+
let n = 3;
102+
let requests = vec![vec![0, 0], vec![1, 2], vec![2, 1]];
103+
104+
let result = 3;
105+
106+
assert_eq!(Solution::maximum_requests(n, requests), result);
107+
}
108+
109+
#[test]
110+
fn test_1601_example_3() {
111+
let n = 5;
112+
let requests = vec![vec![0, 3], vec![3, 1], vec![1, 2], vec![2, 0]];
113+
114+
let result = 4;
115+
116+
assert_eq!(Solution::maximum_requests(n, requests), result);
117+
}
118+
}

0 commit comments

Comments
 (0)