Skip to content

Commit dc6e90a

Browse files
authored
Added solution to 1601 Maximum Number of Achievable Transfer Requests (fishercoder1534#110)
* Adding java solution file for 1601 question Maximum Number of Achievable Transfer Requests * Maximum Number of Achievable Transfer Requests Added solution to this Problem
1 parent a0fbd0d commit dc6e90a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1601|[Maximum Number of Achievable Transfer Requests](https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1601.java) ||Hard|Backtracking|
1112
|1598|[Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1598.java) ||Easy|Stack|
1213
|1592|[Rearrange Spaces Between Words](https://leetcode.com/problems/rearrange-spaces-between-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1592.java) ||Easy|String|
1314
|1588|[Sum of All Odd Length Subarrays](https://leetcode.com/problems/sum-of-all-odd-length-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1588.java) ||Easy|Array|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
int max = 0;
3+
public int maximumRequests(int n, int[][] requests) {
4+
helper(requests, 0, new int[n], 0);
5+
return max;
6+
}
7+
8+
private void helper(int[][] requests, int index, int[] count, int num) {
9+
// Traverse all n buildings to see if they are all 0. (means balanced)
10+
if (index == requests.length) {
11+
for (int i : count) {
12+
if (0 != i) {
13+
return;
14+
}
15+
}
16+
max = Math.max(max, num);
17+
return;
18+
}
19+
// Choose this request
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+
26+
// Not Choose the request
27+
helper(requests, index + 1, count, num);
28+
}
29+
}

0 commit comments

Comments
 (0)