do question 3

This commit is contained in:
2026-04-24 00:27:19 -07:00
parent 8fd66d9681
commit de2aab5e41
+59 -2
View File
@@ -1,4 +1,5 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class MainExercises public class MainExercises
{ {
@@ -150,11 +151,67 @@ public class MainExercises
body to use them instead of arrays. body to use them instead of arrays.
*/ */
private static List<List<Integer>> result;
private static List<Integer> help;
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo result = new ArrayList<>();
return null; help = new ArrayList<>();
findRecursive(n, n);
int[][] answer = casting(result);
return answer;
} }
private static void findRecursive(int target , int max){
if(target == 0){
result.add(new ArrayList<>(help));
return;
}
for(int i = Math.min(target , max) ; i >= 1 ; i--){
help.add(i);
findRecursive(target -i , i);
help.remove(help.size() -1);
}
}
private int[][] casting(List<List<Integer>> input){
int n = input.size();
int[][] answer = new int[n][];
for(int i = 0 ; i < n ; i++){
int k = input.get(i).size();
answer[i] = new int[k];
for(int j = 0 ; j < k ; j++){
answer[i][j] = input.get(i).get(j);
}
}
return answer;
}
public static void main() public static void main()
{ {