Skip to content

Commit da92bcb

Browse files
refactor 1601
1 parent 53a8c29 commit da92bcb

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed
Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
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;
1+
package com.fishercoder.solutions;
2+
3+
public class _1601 {
4+
public static class Solution1 {
5+
int max = 0;
6+
7+
public int maximumRequests(int n, int[][] requests) {
8+
helper(requests, 0, new int[n], 0);
9+
return max;
10+
}
11+
12+
private void helper(int[][] requests, int index, int[] count, int num) {
13+
// Traverse all n buildings to see if they are all 0. (means balanced)
14+
if (index == requests.length) {
15+
for (int i : count) {
16+
if (0 != i) {
17+
return;
18+
}
1419
}
20+
max = Math.max(max, num);
21+
return;
1522
}
16-
max = Math.max(max, num);
17-
return;
23+
// Choose this request
24+
count[requests[index][0]]++;
25+
count[requests[index][1]]--;
26+
helper(requests, index + 1, count, num + 1);
27+
count[requests[index][0]]--;
28+
count[requests[index][1]]++;
29+
30+
// Not Choose the request
31+
helper(requests, index + 1, count, num);
1832
}
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);
2833
}
2934
}

0 commit comments

Comments
 (0)