找到所有可能的组合 - java

时间:2013-04-05 12:06:32

标签: java arrays algorithm combinations

我需要找到以下列方式给出的数据组合,

jobs  #ofIntervals
----  -------------
4     1
1     2
3     2
0     3
2     3

必须根据#ofIntervals制作给定作业的组合集。 输出可能的组合将影响作业的位置。如果有多个具有相同#ofIntervals的作业,则只更改这些作业位置将设置新作业。给定输入的可能结果应该是这样的,

combination-1: 4 1 3 0 2    // same as input

combination-2: 4 3 1 0 2    // here as job 3 and 1 have 2 #ofIntervals they make a new combination 

combination-3: 4 1 3 2 0    // here as job 2 and 0 have 3 #ofIntervals they make a new combination

combination-4: 4 3 1 2 0

任何人都可以帮我写代码或建议算法。

2 个答案:

答案 0 :(得分:3)

  1. 将您的作业分成不同的集合,其中集合中的每个成员具有相同的" #of个间隔"值。
  2. 对于每个集合,生成一个包含该集合的所有permutations的集合。
  3. 生成一个包含第2步中cartesian product个集合的集合。
  4. 最后的收藏是你的解决方案。

答案 1 :(得分:1)

我喜欢mbeckish所写的答案,但这里是我写的实际工作的代码:

import java.util.ArrayList;
import java.util.List;

public class Test
{
    public static void main(String[] args)
    {
        List<JobsInterval> jobsIntervalList = new ArrayList<JobsInterval>();

        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<JobsInterval> list)
    {
        //Assumes the list is already in interval order.
        int currentInterval = -1;
        List<List<JobsInterval>> outerList = new ArrayList<List<JobsInterval>>(list.size());
        List<JobsInterval> 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<JobsInterval>(list.size());
            }

            innerList.add(ji);
        }

        if (null != innerList)
            outerList.add(innerList);

        print(0, outerList, null);
    }

    public static void permute(StringBuilder value, List<JobsInterval> list, List<String> permutations)
    {
        //Check to see if this is the last recursive call
        if (0 == list.size())
        {
            permutations.add(value.toString());
        }
        else
        {
            List<JobsInterval> subList;

            for (int i = 0; i < list.size(); i++)
            {
                subList = new ArrayList<>(list);
                subList.remove(i);
                permute(new StringBuilder(null == value ? "" : value).append(list.get(i).jobs), subList, permutations);
            }
        }
    }

    public static void print(int index, List<List<JobsInterval>> list, StringBuilder value)
    {
        //Check to see if this is the last recursive call
        if (list.size() == index)
            System.out.println(value.toString());
        else
        {
            List<JobsInterval> intervalGroup = list.get(index);
            List<String> permutations = new ArrayList<String>();

            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();
        }
    }
}