-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathCombination.java
67 lines (61 loc) · 2.09 KB
/
Combination.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.thealgorithms.backtracking;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
/**
* Finds all permutations of given array
* @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
*/
public final class Combination {
private Combination() {
}
/**
* Find all combinations of given array using backtracking
* @param arr the array.
* @param n length of combination
* @param <T> the type of elements in the array.
* @return a list of all combinations of length n. If n == 0, return null.
*/
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
if (n < 0) {
throw new IllegalArgumentException("The combination length cannot be negative.");
}
if (n == 0) {
return Collections.emptyList();
}
T[] array = arr.clone();
Arrays.sort(array);
List<TreeSet<T>> result = new LinkedList<>();
backtracking(array, n, 0, new TreeSet<T>(), result);
return result;
}
/**
* Backtrack all possible combinations of a given array
* @param arr the array.
* @param n length of the combination
* @param index the starting index.
* @param currSet set that tracks current combination
* @param result the list contains all combination.
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + n - currSet.size() > arr.length) {
return;
}
if (currSet.size() == n - 1) {
for (int i = index; i < arr.length; i++) {
currSet.add(arr[i]);
result.add(new TreeSet<>(currSet));
currSet.remove(arr[i]);
}
return;
}
for (int i = index; i < arr.length; i++) {
currSet.add(arr[i]);
backtracking(arr, n, i + 1, currSet, result);
currSet.remove(arr[i]);
}
}
}