Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like the answer that mbeckish wrote but here is the code that I wrote to actually do the work:</p> <pre><code>import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List&lt;JobsInterval&gt; jobsIntervalList = new ArrayList&lt;JobsInterval&gt;(); jobsIntervalList.add(new JobsInterval(4, 1)); jobsIntervalList.add(new JobsInterval(1, 2)); jobsIntervalList.add(new JobsInterval(3, 2)); jobsIntervalList.add(new JobsInterval(0, 3)); jobsIntervalList.add(new JobsInterval(2, 3)); printPossibleCombinations(jobsIntervalList); } public static void printPossibleCombinations(List&lt;JobsInterval&gt; list) { //Assumes the list is already in interval order. int currentInterval = -1; List&lt;List&lt;JobsInterval&gt;&gt; outerList = new ArrayList&lt;List&lt;JobsInterval&gt;&gt;(list.size()); List&lt;JobsInterval&gt; innerList = null; //Loop through the list and group them into separate lists by interval. for (JobsInterval ji : list) { if (ji.interval != currentInterval) { if (null != innerList) outerList.add(innerList); currentInterval = ji.interval; innerList = new ArrayList&lt;JobsInterval&gt;(list.size()); } innerList.add(ji); } if (null != innerList) outerList.add(innerList); print(0, outerList, null); } public static void permute(StringBuilder value, List&lt;JobsInterval&gt; list, List&lt;String&gt; permutations) { //Check to see if this is the last recursive call if (0 == list.size()) { permutations.add(value.toString()); } else { List&lt;JobsInterval&gt; subList; for (int i = 0; i &lt; list.size(); i++) { subList = new ArrayList&lt;&gt;(list); subList.remove(i); permute(new StringBuilder(null == value ? "" : value).append(list.get(i).jobs), subList, permutations); } } } public static void print(int index, List&lt;List&lt;JobsInterval&gt;&gt; list, StringBuilder value) { //Check to see if this is the last recursive call if (list.size() == index) System.out.println(value.toString()); else { List&lt;JobsInterval&gt; intervalGroup = list.get(index); List&lt;String&gt; permutations = new ArrayList&lt;String&gt;(); permute(null, intervalGroup, permutations); for (String permutation : permutations) print(index+1, list, new StringBuilder(null == value ? "" : value).append(permutation)); } } private static class JobsInterval { public int jobs; public int interval; public JobsInterval(int j, int i) { jobs = j; interval = i; } public String toString() { return new StringBuilder().append('{').append(jobs).append(", ").append(interval).append('}').toString(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload