Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b3da244

Browse files
authored
Adding java solution file for 1601 question
Maximum Number of Achievable Transfer Requests
1 parent f4ce5b3 commit b3da244

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
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)