Skip to content

Instantly share code, notes, and snippets.

@jbleduigou
Last active December 11, 2019 21:46
Show Gist options
  • Save jbleduigou/4a46a80b5cbeac167931c54fb51dd056 to your computer and use it in GitHub Desktop.
Save jbleduigou/4a46a80b5cbeac167931c54fb51dd056 to your computer and use it in GitHub Desktop.
// Generate all possible sub-lists (PowerSet algorithm)
Set<List<Item>> subLists = generateAllSubLists(items);
// Find best solution for each sub-set
SortedSet<Solution> solutions = new TreeSet<>();
for (List<Item> sublist : subLists) {
Solution solution = findBestSolution(maxWeight, sublist);
solutions.add(solution);
}
// No need to sort solutions, TreeSet implements SortedSet
Solution best = solutions.first();
System.err.println(best);
System.out.println(best.getPrice());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment