这个问题已经困扰我好几天了,而我却无所适从。我已经非常努力地自己解决了这个问题,但是现在,我非常感谢您的帮助和朝着正确方向的指点。
问题:
给出一组数字,并且每个数字可以大于或小于以下数字的最大限制,请根据限制确定数字的有效顺序数。
示例:
数字:20、30、36、40
一个数字可以大于以下数字的最大数量:16
一个数字可以小于以下数字的最大数量:8
这里将有3个有效的订单:
36 40 30 20
40 36 30 20
40 30 36 20
我已经设计了一种使用递归和树生成所有有效排列的方法,但是不幸的是,如果列表中有许多有效顺序(我相信方法是n!运行时),它会花费太长时间。我觉得好像有一种更快,更数学的方法来使用我刚才没有看到的组合方法来解决这个问题。任何建议将不胜感激,谢谢!
编辑: 这是我想出的置换算法的代码。代码的最后部分使用上面给出的示例对其进行了测试。它是用Python 3.6编写的。
class block:
def __init__(self, val, children):
self.val = val
self.children = children
# Gets all the possible children of the current head within the limits
def get_children(head, nums, b, visited, u, d):
global total
if all(visited):
total += 1
return
for i in range(b):
if not visited[i]:
if head.val - nums[i] <= d and nums[i] - head.val <= u:
head.children.append(block(nums[i], []))
visited[i] = True
get_children(head.children[-1], nums, b, visited, u, d)
visited[i] = False
# Display all the valid permutations of the current head
def show(head, vals, b):
vals.append(head.val)
if head.children == [] and len(vals) == b:
print(*vals)
return
for child in head.children:
show(child, vals[:], b)
# Test it out with the sample
b, nums, u, d = 4, [20, 30, 36, 40], 8, 16
visited = [False for x in range(b)]
total = 0
heads = []
for i in range(b):
heads.append(block(nums[i], []))
visited[i] = True
get_children(heads[-1], nums, b, visited, u, d)
visited[i] = False
show(heads[-1], [], b)
print(total)
此打印:
36 40 30 20
40 30 36 20
40 36 30 20
3
答案 0 :(得分:3)
尝试使用10个相等的数字进行计算的方法导致运行时间为35秒。
我注意到的第一件事是该函数只需要列表头中的最后一个条目,因此可以简化该函数以使用整数而不是列表。以下代码具有三个简化:
简化的代码如下:
def get_children(head, nums, b, visited, u, d):
if all(visited):
return 1
t = 0
for i in range(b):
if not visited[i]:
if head - nums[i] <= d and nums[i] - head <= u:
head2 = nums[i]
visited[i] = True
t += get_children(head2, nums, b, visited, u, d)
visited[i] = False
return t
# Test it out with the sample
nums, u, d = [20, 30, 36, 40], 8, 16
b = len(nums)
visited = [False for x in range(b)]
total = 0
for i in range(b):
head = nums[i]
visited[i] = True
total += get_children(head, nums, b, visited, u, d)
visited[i] = False
print(total)
这需要7秒才能显示10个相等的数字。
我注意到的第二件事是(对于特定的测试用例)get_children的返回值仅取决于被访问的True和head的值。
因此,我们可以缓存结果以避免重新计算它们:
cache={}
# Gets all the possible children of the current head within the limits
def get_children(head, nums, b, visited, u, d):
if all(visited):
return 1
key = head,sum(1<<i for i,v in enumerate(visited) if v)
result = cache.get(key,None)
if result is not None:
return result
t = 0
for i in range(b):
if not visited[i]:
if head - nums[i] <= d and nums[i] - head <= u:
head2 = nums[i]
visited[i] = True
t += get_children(head2, nums, b, visited, u, d)
visited[i] = False
cache[key] = t
return t
此版本仅需0.03秒即可得到10个相等的数字(即比原始数字快1000倍)。
如果您要处理多个具有不同b / u / d值的测试用例,则应在每个测试用例的开始处重置缓存(即cache = {})。
答案 1 :(得分:1)
正如评论中所指出的,在此处找到所有有效置换等同于在有向图中标识所有哈密顿路径,这些路径具有将您的数字作为顶点和边,对应于允许彼此跟随的每对数字。 / p>
这是一个非常简单的Java(IDEOne)程序,用于查找此类路径。这是否使问题易于处理取决于图形的大小和分支因子。
public static void main(String[] args)
{
int[] values = {20, 30, 36, 40};
Vertex[] g = new Vertex[values.length];
for(int i=0; i<g.length; i++)
g[i] = new Vertex(values[i]);
for(int i=0; i<g.length; i++)
for(int j=0; j<g.length; j++)
if(i != j && g[j].id >= g[i].id-16 && g[j].id <= g[i].id+8)
g[i].adj.add(g[j]);
Set<Vertex> toVisit = new HashSet<>(Arrays.asList(g));
LinkedList<Vertex> path = new LinkedList<>();
for(int i=0; i<g.length; i++)
{
path.addLast(g[i]);
toVisit.remove(g[i]);
findPaths(g[i], path, toVisit);
toVisit.add(g[i]);
path.removeLast();
}
}
static void findPaths(Vertex v, LinkedList<Vertex> path, Set<Vertex> toVisit)
{
if(toVisit.isEmpty())
{
System.out.println(path);
return;
}
for(Vertex av : v.adj)
{
if(toVisit.contains(av))
{
toVisit.remove(av);
path.addLast(av);
findPaths(av, path, toVisit);
path.removeLast();
toVisit.add(av);
}
}
}
static class Vertex
{
int id;
List<Vertex> adj;
Vertex(int id)
{
this.id = id;
adj = new ArrayList<>();
}
public String toString()
{
return String.valueOf(id);
}
}
输出:
[36, 40, 30, 20]
[40, 30, 36, 20]
[40, 36, 30, 20]