-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
/** | ||
|
@@ -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) { | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List doesn't seem to have a removeLast method There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.