算法可变大小项目兼顾到大致平衡的套算法、项目

2023-09-11 04:12:03 作者:午夜嗨情歌

我正在寻找一种算法来拆分不同大小成N的数同样大小群的项目的列表。

具体而言,我在C#中的ASP.NET网站工作在那里我有一个字符串(数据库检索)名单。该字符串是长短不一的。我有一组其需要显示的字符串列。我需要一个算法,将找到最平衡的套(项目顺序是不相关),以便最终列要尽可能平衡。

抽象例:

创建3列。

产品分配:

   - 项目A  - 身高5
  -  B项 - 高3
  -  C项 - 身高7
  -  D项 - 身高2
  -  E项 - 高3
 

所需的输出:

 专栏1:项目A,D项
专栏2:C项
第3栏:项目B,E项
 
红黑树插入算法实现原理分析

解决方案

做的最快的是可能只是将每个新项目到最小的名单(其中最小是所有项目的大小之和列表)。

I'm seeking an algorithm to split a list of items of varying sizes into "N" number of similarly-sized groups.

Specifically, I'm working on an ASP.NET site in C# where I have a (database-retrieved) list of strings. The strings are of varying lengths. I have a set of columns which need to display the strings. I need an algorithm that will find the most balanced sets (item order is irrelevant) to allow the final columns to be as balanced as possible.

Abstracted Example:

Creating 3 columns.

Items to distribute:

 - Item A - height 5
 - Item B - height 3
 - Item C - height 7
 - Item D - height 2
 - Item E - height 3

Desired output:

Column 1: Item A, Item D
Column 2: Item C
Column 3: Item B, Item E

解决方案

The quickest thing to do is probably just insert each new item into the smallest list (where "smallest" is the sum of the sizes of all the items in the list).