Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change List<Integer> to LinkedList<Integer> and remove 'n < 0' #5439

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.thealgorithms.backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
Expand All @@ -18,16 +19,16 @@ private ArrayCombination() {
* @return A list of all combinations of length k.
*/
public static List<List<Integer>> combination(int n, int k) {
if (n < 0 || k < 0 || k > n) {
if (k < 0 || k > n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The n < 0 check is important because it ensures that the input values are valid within the context of combinations, prevents undefined behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like if you remove this check everything will work correctly. And you're right, we can remove it. But this check provides readable constraints and with this check the code becomes more readable.

throw new IllegalArgumentException("Wrong input.");
}

List<List<Integer>> combinations = new ArrayList<>();
combine(combinations, new ArrayList<>(), 0, n, k);
combine(combinations, new LinkedList<>(), 0, n, k);
return combinations;
}

private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
private static void combine(List<List<Integer>> combinations, LinkedList<Integer> current, int start, int n, int k) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using LinkedList instead of List in method parameters is considered bad practice as it provides tight coupling, and issues with extensibility.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List doesn't seem to have a removeLast method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has. In code below, you can see that it already used. And code compile and pass tests.

if (current.size() == k) { // Base case: combination found
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
return;
Expand Down