Skip to content

Commit 54175da

Browse files
committed
refactor: change List<Integer> to LinkedList<Integer> and remove 'n < 0'
1 parent b396a97 commit 54175da

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/main/java/com/thealgorithms/backtracking/ArrayCombination.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.backtracking;
22

33
import java.util.ArrayList;
4+
import java.util.LinkedList;
45
import java.util.List;
56

67
/**
@@ -18,16 +19,16 @@ private ArrayCombination() {
1819
* @return A list of all combinations of length k.
1920
*/
2021
public static List<List<Integer>> combination(int n, int k) {
21-
if (n < 0 || k < 0 || k > n) {
22+
if (k < 0 || k > n) {
2223
throw new IllegalArgumentException("Wrong input.");
2324
}
2425

2526
List<List<Integer>> combinations = new ArrayList<>();
26-
combine(combinations, new ArrayList<>(), 0, n, k);
27+
combine(combinations, new LinkedList<>(), 0, n, k);
2728
return combinations;
2829
}
2930

30-
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
31+
private static void combine(List<List<Integer>> combinations, LinkedList<Integer> current, int start, int n, int k) {
3132
if (current.size() == k) { // Base case: combination found
3233
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
3334
return;

0 commit comments

Comments
 (0)