Skip to content

Commit 6940c81

Browse files
committed
Add solution #1601
1 parent 4e4fd9f commit 6940c81

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,411 LeetCode solutions in JavaScript
1+
# 1,412 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1236,6 +1236,7 @@
12361236
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12371237
1599|[Maximum Profit of Operating a Centennial Wheel](./solutions/1599-maximum-profit-of-operating-a-centennial-wheel.js)|Medium|
12381238
1600|[Throne Inheritance](./solutions/1600-throne-inheritance.js)|Medium|
1239+
1601|[Maximum Number of Achievable Transfer Requests](./solutions/1601-maximum-number-of-achievable-transfer-requests.js)|Hard|
12391240
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12401241
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12411242
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1601. Maximum Number of Achievable Transfer Requests
3+
* https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/
4+
* Difficulty: Hard
5+
*
6+
* We have n buildings numbered from 0 to n - 1. Each building has a number of employees.
7+
* It's transfer season, and some employees want to change the building they reside in.
8+
*
9+
* You are given an array requests where requests[i] = [fromi, toi] represents an employee's
10+
* request to transfer from building fromi to building toi.
11+
*
12+
* All buildings are full, so a list of requests is achievable only if for each building, the
13+
* net change in employee transfers is zero. This means the number of employees leaving is
14+
* equal to the number of employees moving in. For example if n = 3 and two employees are
15+
* leaving building 0, one is leaving building 1, and one is leaving building 2, there should
16+
* be two employees moving to building 0, one employee moving to building 1, and one employee
17+
* moving to building 2.
18+
*
19+
* Return the maximum number of achievable requests.
20+
*/
21+
22+
/**
23+
* @param {number} n
24+
* @param {number[][]} requests
25+
* @return {number}
26+
*/
27+
var maximumRequests = function(n, requests) {
28+
let maxAchievable = 0;
29+
tryCombination(0, 0, new Array(n).fill(0));
30+
return maxAchievable;
31+
32+
function tryCombination(index, count, balance) {
33+
if (index === requests.length) {
34+
if (balance.every(val => val === 0)) {
35+
maxAchievable = Math.max(maxAchievable, count);
36+
}
37+
return;
38+
}
39+
40+
const [from, to] = requests[index];
41+
balance[from]--;
42+
balance[to]++;
43+
tryCombination(index + 1, count + 1, balance);
44+
balance[from]++;
45+
balance[to]--;
46+
47+
tryCombination(index + 1, count, balance);
48+
}
49+
};

0 commit comments

Comments
 (0)