Skip to content

Commit

Permalink
Added generate combinations of trucks and cities
Browse files Browse the repository at this point in the history
  • Loading branch information
gocklkatz committed Jul 19, 2024
1 parent ec95d90 commit 7f01924
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/io/gocklkatz/m12s/utils/CombinatoricHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ private static void powerSetInternal(List<Character> set, List<List<Character>>
}
}

/*
* Generate all different combinations of distributing n items into m buckets
*/
public static List<List<Integer>> generateCombinations(int n, int m) {
List<List<Integer>> combinations = new ArrayList<>();
int[] indices = new int[n];

while (true) {
// Add current combination to the list
List<Integer> combination = new ArrayList<>();
for (int index : indices) {
combination.add(index);
}
combinations.add(combination);

// Find the rightmost index that can be incremented
int i;
for (i = n - 1; i >= 0; i--) {
if (indices[i] < m - 1) {
indices[i]++;
break;
} else {
indices[i] = 0;
}
}

// If no index could be incremented, we are done
if (i < 0) {
break;
}
}

return combinations;
}

/*
* Generate n by n matrix with random values 0 <= x < 10
*/
Expand Down

0 comments on commit 7f01924

Please sign in to comment.