给定任意数量的有序列表
List<int> list1 = new List<int> {1, 1, 2};
List<int> list2 = new List<int> {1, 2, 3, 4};
List<int> list3 = new List<int> {1, 2};
List<int> listN = new List<int> {....,....};
我希望找到列表的组合,使得每个组合的总和按升序排列。例如:
{1, 1, 1} = 3, where {1 (1st element of list1), 1 (1st element of list2), 1 (1st element of list3)}
{1, 1, 1} = 3, where {1 (2nd element of list1), 1 (1st element of list2, 1 (1st element of list3)}
{1, 2, 1} = 4
{1, 1, 2} = 4
{2, 1, 1} = 4
...
找到升序求和的原因是我可以选择仅计算前M个组合(例如,上面的M = 5)。
我目前的想法是通过从列表的一个小子集开始,其中当前元素和下一个元素之间的差为0,来扩展查找所有列表的组合,如Combination of List<List<int>>中讨论的那样。
List<int> tempList1 = new List<int> {1, 1};
List<int> tempList2 = new List<int> {1};
List<int> tempList3 = new List<int> {1};
找到所有组合,然后将下一个具有最小差异的元素添加到列表中。
List<int> tempList1 = new List<int> {1, 1, 2};
List<int> tempList2 = new List<int> {1, 2};
List<int> tempList3 = new List<int> {1, 2};
从那里构建解决方案集是否可行?有更好的方法吗?
4
,因为list1
有两个相同的元素(两个1
),所以我认为你的问题应该反映出来。 - Jeppe Stig Nielsen