forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_544.java
40 lines (35 loc) · 1.06 KB
/
_544.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _544 {
public String findContestMatch(int n) {
List<String> pairs = new ArrayList<>();
int left = 1;
int right = n;
while (left < right) {
pairs.add("(" + left + "," + right + ")");
left++;
right--;
}
if (n == 2) {
return pairs.get(0);
}
return generateFinal(pairs, n / 2);
}
private String generateFinal(List<String> pairs, int n) {
if (n > 2) {
int size = pairs.size();
int left = 0;
int right = size - 1;
List<String> newPairs = new ArrayList<>();
while (left < right) {
String newPair = "(" + pairs.get(left) + "," + pairs.get(right) + ")";
newPairs.add(newPair);
left++;
right--;
}
return generateFinal(newPairs, n / 2);
}
return "(" + pairs.get(0) + "," + pairs.get(1) + ")";
}
}